Skip to content

Commit

Permalink
feat(router): Basic routing
Browse files Browse the repository at this point in the history
  • Loading branch information
alvaropinot committed May 7, 2024
1 parent 7705f88 commit 6cef200
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
;(function (Deno, { mode }) {
const EXTENSIONS = {
json: 'application/json; charset=utf-8',
'': 'text/html',
html: 'text/html',
ico: 'ico',
}

Deno.serve(async (req) => {

const url = new URL(req.url)
const extension = [...url.pathname].join('').split('.')[1]
// TODO: add error handling
const text = await Deno.readTextFile('.' + url.pathname)

if (mode === 'debug') {]
console.log('Method:', req.method)
console.log('Path:', url.pathname)
console.log('Exntension:', extension)
console.log('Query parameters:', url.searchParams)
console.log('Headers:', req.headers)
console.log('File contents"', text)
if (req.body) {
const body = await req.text()
console.log('Body:', body)
}
}

return new Response(text, {
status: 200,
headers: {
'content-type': EXTENSIONS[extension] || '',
},
})
})
})(Deno, { mode: 'debug' })

0 comments on commit 6cef200

Please sign in to comment.