Very simple deno webserver mostly exactly from the example in the docs at https://deno.land/manual/examples/http_server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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