Skip to content

Instantly share code, notes, and snippets.

@myobie
Created July 29, 2021 18:25
Show Gist options
  • Save myobie/d6a3dc45a0f3ded8dbd17bca76cbcf44 to your computer and use it in GitHub Desktop.
Save myobie/d6a3dc45a0f3ded8dbd17bca76cbcf44 to your computer and use it in GitHub Desktop.
Very simple deno webserver mostly exactly from the example in the docs at https://deno.land/manual/examples/http_server
// Start listening on port 8080 of localhost.
const server = Deno.listen({ port: 8080 })
console.log('HTTP webserver running...')
const headers = Object.freeze({
'Content-type': 'plain/text'
})
for await (const conn of server) {
// deno-lint-ignore no-extra-semi
;(async () => {
const httpConn = Deno.serveHttp(conn)
for await (const requestEvent of httpConn) {
const body = `Your user-agent is:\n\n${requestEvent.request.headers.get('user-agent') ?? 'Unknown'}`
const resp = new Response(body, { status: 200, headers })
requestEvent.respondWith(resp)
}
})()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment