https: Local HTTPS for development

Tags: til https ssl

When developing a browser based application (client side JS), it's very useful to serve it to to your browser and use the excellent browser development tools. There are a lots of basic HTTP servers (e.g. python -m http-server)that can serve up a directory of static files, and that works for most applications. Unfortunately there are browser APIs that only work when the page is served in a secure context e.g. deviceorientation. For that scenario it's helpful to use self signed certificates and serve the directory via HTTPS.

openssl can generate a self signed cert for a given domain (server.key and server.crt):

openssl req \
  -newkey rsa:2048 \
  -x509 \
  -nodes \
  -keyout server.key \
  -new \
  -out server.crt \
  -subj /CN=$(DUMMY_HOSTNAME) \
  -days 3650

Then the npm package http-server can use those to serve a directory over HTTPS:

npx http-server -S -C server.crt -K server.key

Now accessing the page via a valid IP or the DUMMY_HOSTNAME provided in the cert will allow the browser to use the more sensitive browser APIs e.g. deviceorientation.

Published on: 06 Sep 2025