Module

std/http/server.ts

Deno standard library
Go to Latest
function listenAndServe
import { listenAndServe } from "https://deno.land/std@0.107.0/http/server.ts";

Constructs a server, creates a listener on the given address, accepts incoming connections, and handles requests on these connections with the given handler.

If the port is omitted from the address, :80 is used.

If the host is omitted from the address, the non-routable meta-address 0.0.0.0 is used.

import { listenAndServe } from "https://deno.land/std@0.107.0/http/server.ts";

const addr = ":4505";

console.log("server listening on http://localhost:4505");

await listenAndServe(addr, (request) => {
  const body = `Your user-agent is:\n\n${request.headers.get(
    "user-agent",
  ) ?? "Unknown"}`;

  return new Response(body, { status: 200 });
});

Parameters

addr: string

The address to listen on.

handler: Handler

The handler for individual HTTP requests.

optional
options: ServeInit

Optional serve options.

Returns

Promise<void>