Skip to main content
Deno 2 is finally here 🎉️
Learn more
Go to Latest
The Standard Library has been moved to JSR. See the blog post for details.
method Server.prototype.listenAndServeTls
import { Server } from "https://deno.land/std@0.114.0/http/mod.ts";

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 with the port omitted from the address, :443 is used.

If the server was constructed with the host omitted from the address, 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.114.0/http/server.ts";

const addr = ":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({ addr, 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);

Parameters

certFile: string

The path to the file containing the TLS certificate.

keyFile: string

The path to the file containing the TLS private key.

Returns

Promise<void>