D
Deno

help

Serve Fresh over HTTPS locally

Rrabbit_rabbit4/19/2023
How can I serve Fresh over HTTPS locally? In node I might generate certs and do
https.createServer({
key: fs.readFileSync(path.join(__dirname, '..', 'certs/localhost.key'), 'utf8').toString(),
cert: fs.readFileSync(path.join(__dirname, '..', 'certs/localhost.crt'), 'utf8').toString(),
}).listen(8000)
https.createServer({
key: fs.readFileSync(path.join(__dirname, '..', 'certs/localhost.key'), 'utf8').toString(),
cert: fs.readFileSync(path.join(__dirname, '..', 'certs/localhost.crt'), 'utf8').toString(),
}).listen(8000)
What would the equivalent be for Fresh? Thanks in advance! In case anyone else runs into this, my workaround was to update my main.ts file to conditionally call serveTls like so
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { ServerContext, start } from "$fresh/server.ts";
import { serveTls } from "https://deno.land/std@0.184.0/http/server.ts";
import manifest from "./fresh.gen.ts";

import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";

const port = parseInt(Deno.env.get("PORT") || "8000", 10);

const opts = { port, plugins: [twindPlugin(twindConfig)] };

if (Deno.env.get("RUN_HTTPS") === "https://localhost:8000") {
const ctx = await ServerContext.fromManifest(manifest, opts);
await serveTls(ctx.handler(), {
port,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
});
} else {
await start(manifest, opts);
}
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />

import { ServerContext, start } from "$fresh/server.ts";
import { serveTls } from "https://deno.land/std@0.184.0/http/server.ts";
import manifest from "./fresh.gen.ts";

import twindPlugin from "$fresh/plugins/twind.ts";
import twindConfig from "./twind.config.ts";

const port = parseInt(Deno.env.get("PORT") || "8000", 10);

const opts = { port, plugins: [twindPlugin(twindConfig)] };

if (Deno.env.get("RUN_HTTPS") === "https://localhost:8000") {
const ctx = await ServerContext.fromManifest(manifest, opts);
await serveTls(ctx.handler(), {
port,
certFile: "./local-certs/localhost.crt",
keyFile: "./local-certs/localhost.key",
});
} else {
await start(manifest, opts);
}
Rrabbit_rabbit4/19/2023
To make your own certificates for localhost, these instructions work nicely: https://letsencrypt.org/docs/certificates-for-localhost/
Certificates for localhost
Sometimes people want to get a certificate for the hostname “localhost”, either for use in local development, or for distribution with a native application that needs to communicate with a web application. Let’s Encrypt can’t provide certificates for “localhost” because nobody uniquely owns it, and it’s not rooted in a top level domain like “.co...
MGMark G4/21/2023
I highly recommend mkcert as the easiest way to create certs for localhost... https://github.com/FiloSottile/mkcert
GitHub
GitHub - FiloSottile/mkcert: A simple zero-config tool to make loca...
A simple zero-config tool to make locally trusted development certificates with any names you&#39;d like. - GitHub - FiloSottile/mkcert: A simple zero-config tool to make locally trusted develo...

Looking for more? Join the community!