Serve a folder over HTTP in one line
When I need to preview a static site or share a folder on my network for a minute, I do not want to set up a project. These one-liners serve the current directory over HTTP with tools that are already installed.
Python (already on macOS and most Linux)
python3 -m http.server 8000
Then open http://localhost:8000. Heads up: by default it already listens on every interface, so anyone on your network can reach it. When I only want it for myself, I bind it to loopback:
python3 -m http.server 8000 --bind 127.0.0.1
Node, without installing anything
npx serve .
npx fetches serve on the fly and runs it. It prints both a local and a network URL, and handles single-page-app routing better than the Python one.
PHP, if it happens to be around
php -S localhost:8000
Which one I pick
For a plain folder of files, the Python server is the fastest thing that is always there. For anything that behaves like an app (client-side routing, clean URLs), npx serve saves me the headaches.
More in How-to