Hi there! Are you looking for the official Deno documentation? Try docs.deno.com for all your Deno learning needs.

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);
}
function serveHttp(conn: Conn): HttpConn;
§
serveHttp(conn: Conn): HttpConn
[src]

§Parameters

§
conn: Conn
[src]