Localhost dev server

Your own little fake internet

November 26, 2018 — October 6, 2021

computers are awful
diy
doing internet
Figure 1

I am building some HTML page. I can do some things with an HTML files on my hard disk, although these are locked down for security reasons. So I run a local server to serve those files and have my browser treat them like part of the real internet.

Usually for local testing, serving the files unencrypted (plain HTTP) is enough; sometimes for weird edge-case reasons I need encrypted (keywords: HTTPS/SSL/TLS) which is a whole ’nother thing.

1 One-liners

There is a sport of writing HTTP server one-liners, and a mini site for them. Here are some strategic selections from those.

1.1 bareback

Bareback feature-free somehow-works HTTP with netcat:

nc -l 127.0.0.1 8889 < foo.html

1.2 python

This is reasonably simple and serves static files with little fuss.

python3 -m http.server --bind 127.0.0.1

2 VS Code

Live Preview runs a sophisticated integrated server inside VS code.

3 site.js

Site.js is one of several neat tools that integrates hugo.

One person.

Develop and test on your own device. One server.

Sync and deploy to your own VPS. One site.

Host your own site at your own domain

4 Caddy

Caddy, is a free and open source server, and that will get you a long way. It has automatic SSL. It supporting plugins for totally OTT features such as AWS lambda function proxying and automatic hugo for plain text blogging.

caddy file-server --listen 127.0.0.1:2019

5 npm servers

For node browser apps, e.g. beefy and run-browser and their ilk might work and probably integrate with your build process. There are so many different options for node that I usually pass over it in choice-paralyzed silence.

Fenix is a GUI wrapped around a node-style server that promises advanced debugging/introspection features. But I haven’t used it and it doesn’t run on Linux, so ignoring for now.

For netlify users in particular there is the netlify dev server

netlify dev --live

It has many features including draft deployments and ssh tunnel configuration for private preview.

6 R

As mentioned at R HTML slides I use this CLI script, which is has a couple of useful features such as auto-watching for file changes.

#!/usr/bin/env Rscript
library(servr)
port=4000
cmdArgs = commandArgs(trailingOnly = TRUE)
if (length(cmdArgs>0)){
    port = strtoi(cmdArgs[1])
}
servr::httw(port=port)
browseURL(paste("http://127.0.0.1:", port, sep="")

I save it in serve.R and execute it as

serve.R 4000

7 Oh wait but I need encryption

Crap. I understand, but this is tricky in general. Perhaps you don’t truly need SSL? Most SSL-secured things are also available without SSL on localhost domain. Failing that, Caddy is probably easiest. See bonus tips under secure servers.

8 Bonus time: tunneling

ngrok is a service that allows you to inspect and replay web service requests from the internet to a local server for analysis.

9 Special case: woof

Woof transfers one file then quits.