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

x/frugal/docs/dep/std/http.ts>serveTls

A frugal web framework
Go to Latest
function serveTls
Re-export
import { serveTls } from "https://deno.land/x/frugal@0.5.0/docs/dep/std/http.ts";

Serves HTTPS requests with the given handler.

You must specify key or keyFile and cert or certFile options.

You can specify an object with a port and hostname option, which is the address to listen on. The default is port 8443 on hostname "0.0.0.0".

The below example serves with the default port 8443.

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

const cert = "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----\n";
const key = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n";
serveTls((_req) => new Response("Hello, world"), { cert, key });

// Or

const certFile = "/path/to/certFile.crt";
const keyFile = "/path/to/keyFile.key";
serveTls((_req) => new Response("Hello, world"), { certFile, keyFile });

serveTls function prints the message Listening on https://<hostname>:<port>/ on start-up by default. If you like to change this message, you can specify onListen option to override it.

import { serveTls } from "https://deno.land/std@0.224.0/http/server.ts";
const certFile = "/path/to/certFile.crt";
const keyFile = "/path/to/keyFile.key";
serveTls((_req) => new Response("Hello, world"), {
  certFile,
  keyFile,
  onListen({ port, hostname }) {
    console.log(`Server started at https://${hostname}:${port}`);
    // ... more info specific to your server ..
  },
});

You can also specify undefined or null to stop the logging behavior.

import { serveTls } from "https://deno.land/std@0.224.0/http/server.ts";
const certFile = "/path/to/certFile.crt";
const keyFile = "/path/to/keyFile.key";
serveTls((_req) => new Response("Hello, world"), {
  certFile,
  keyFile,
  onListen: undefined,
});

Parameters

handler: Handler

The handler for individual HTTPS requests.

options: ServeTlsInit

The options. See ServeTlsInit documentation for details.