Deno.serveHttp
Services HTTP requests given a TCP or TLS socket.
const conn = Deno.listen({ port: 80 });
const httpConn = Deno.serveHttp(await conn.accept());
const e = await httpConn.nextRequest();
if (e) {
e.respondWith(new Response("Hello World"));
}
If httpConn.nextRequest()
encounters an error or returns null
then the underlying HttpConn resource is closed automatically.
Alternatively, you can also use the Async Iterator approach:
async function handleHttp(conn: Deno.Conn) {
for await (const e of Deno.serveHttp(conn)) {
e.respondWith(new Response("Hello World"));
}
}
for await (const conn of Deno.listen({ port: 80 })) {
handleHttp(conn);
}