Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/pogo/dependencies.ts>http.Server#serve

Server framework for Deno
Latest
method http.Server.prototype.serve
import { http } from "https://deno.land/x/pogo@v0.6.0/dependencies.ts";
const { Server } = http;

Accept incoming connections on the given listener, and handle requests on these connections with the given handler.

HTTP/2 support is only enabled if the provided Deno.Listener returns TLS connections and was configured with "h2" in the ALPN protocols.

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

Will always close the created listener.

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

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({ handler });
const listener = Deno.listen({ port: 4505 });

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

await server.serve(listener);

Parameters

listener: Deno.Listener

The listener to accept connections from.