Skip to main content
Module

x/cav/deps.ts>http.Server#listenAndServe

A server framework for Deno
Go to Latest
method http.Server.prototype.listenAndServe
import { http } from "https://deno.land/x/cav@0.0.21/deps.ts";
const { Server } = http;

Create a listener on the server, accept incoming connections, and handle requests on these connections with the given handler.

If the server was constructed without a specified port, 80 is used.

If the server was constructed with the hostname omitted from the options, the non-routable meta-address 0.0.0.0 is used.

Throws a server closed error if the server has been closed.

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

const port = 4505;
const handler = (request: Request) => {
  const body = `Your user-agent is:\n\n${request.headers.get(
   "user-agent",
  ) ?? "Unknown"}`;

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

const server = new Server({ port, handler });

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

await server.listenAndServe();

Returns

Promise<void>