Skip to main content
Module

x/netzo/deno.d.ts>Deno.serveHttp

SDK for Netzo, the open Web platform to unify IoT devices, applications and services.
Go to Latest
function Deno.serveHttp
import { Deno } from "https://deno.land/x/netzo@v0.1.10/deno.d.ts";
const { serveHttp } = Deno;

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