Sometimes you want to serve local html files through a webserver

The browser can open local files via file:// but that’s just not the same thing at serving over http. In several regards, including CORS restrictions, going through a real web server is more ideal and better serves one’s debugging needs

Historically, we’re a fan of Python’s SimpleHTTPServer - just run python -m SimpleHTTPServer 8080 in the directory you want to serve and voilà: visiting http://localhost:8080 in a browser of choice gives you exactly what you need.

Choose the directory carefully though, since anyone on the same LAN as you can now read all the files in that directory including subdirectories!

MacOS no longer ships with Python!

The one-liner above used to work out-of-the-box on any Mac, up until a few years ago when they dropped Python from the default feature-set

% python
zsh: command not found: python

We can of course install Python ourselves, but that’s way too clunky of an approach if we don’t need Python for anything else.

What can we use instead? This article enumerates a few alternatives in different scripting languages. Are any of them viable out of the box on macOS Monterey or Ventura?

Node.js?

% node
zsh: command not found: node

No luck.

PHP?

% php
zsh: command not found: php

Nope.

Python 3?

 % python3
Python 3.9.10 (main, Jan 15 2022, 11:40:36) 
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 

Oh, this machine actually has Python 3, but…

% which python3
/opt/homebrew/bin/python3

…seems it was installed using brew, so that violates our out of the box requirement.

Ruby?

% ruby

Yes!?

^Cruby: Interrupt

% which ruby
/usr/bin/ruby

Ruby is still shipping with macOS!

That means we can replace our old Python SimpleHTTPServer habits with the following:

ruby -run -e httpd . -p 8080

Single line, nothing to install, pure joy!




sponsor

Need private photo sharing that's not tied up in the social media circus? Photoroll is a good way to share photos with your clients.
Or privately gather media from participants of weddings and other small to medium-sized events.