import { Deno } from "https://deno.land/x/tl_log@0.1.2/examples/deploy.d.ts";
const { serveHttp } = Deno;
Services HTTP requests given a TCP or TLS socket.
const conn = await Deno.connect({ port: 80, hostname: "127.0.0.1" });
const httpConn = Deno.serveHttp(conn);
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);
}
Parameters
conn: Conn