Skip to main content
Deno.serveHttp - Deno documentation
function Deno.serveHttp
serveHttp(conn: Conn): HttpConn
Deprecated

This will be soft-removed in Deno 2.0. See the Deno 1.x to 2.x Migration Guide for migration instructions.

Provides an interface to handle HTTP request and responses over TCP or TLS connections. The method returns an HttpConn which yields up RequestEvent events, which utilize the web platform standard Request and Response objects to handle the request.

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"));
}

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);
}

If httpConn.nextRequest() encounters an error or returns null then the underlying HttpConn resource is closed automatically.

Also see the experimental Flash HTTP server Deno.serve which provides a ground up rewrite of handling of HTTP requests and responses within the Deno CLI.

Note that this function consumes the given connection passed to it, thus the original connection will be unusable after calling this. Additionally, you need to ensure that the connection is not being used elsewhere when calling this function in order for the connection to be consumed properly.

For instance, if there is a Promise that is waiting for read operation on the connection to complete, it is considered that the connection is being used elsewhere. In such a case, this function will fail.

Parameters

conn: Conn

Return Type