W3cubDocs

/Deno

Deno.serveHttp

Services HTTP requests given a TCP or TLS socket.

const conn = await 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

Parameters

conn: Conn

Return Type

© 2018–2021 the Deno authors
https://doc.deno.land/deno/stable/~/Deno.serveHttp