Skip to content

Instantly share code, notes, and snippets.

@myobie
Created Jul 29, 2021
Embed
What would you like to do?
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