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

x/lume/deps/http.ts>Server

🔥 Static site generator for Deno 🦕
Very Popular
Go to Latest
class Server
Deprecated
Deprecated

(will be removed after 1.0.0) Use Deno.serve instead.

Used to construct an HTTP server.

import { Server } from "https://deno.land/x/lume@v1.19.4/deps/http.ts";

Constructors

new
Server(serverInit: ServerInit)

Constructs a new HTTP Server instance.

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

Properties

readonly
addrs: Deno.Addr[]

Get the list of network addresses the server is listening on.

readonly
closed: boolean

Get whether the server is closed.

Methods

Immediately close the server listeners and associated HTTP connections.

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

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();
listenAndServeTls(certFile: string, keyFile: string)

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

If the server was constructed without a specified port, 443 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 });

const certFile = "/path/to/certFile.crt";
const keyFile = "/path/to/keyFile.key";

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

await server.listenAndServeTls(certFile, keyFile);
serve(listener: Deno.Listener)

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