serve - A Simple File Webserver for the Command Line

Apr 7, 2023

I wanted a simple webserver that simply serves the files in a directory. There are plenty of options, but I wanted one that doesn’t depend on other programs (like NPM/NPX/Python/…). So I built one.

I proudly present serve! A simple webserver that can be started from the command line.

Just do this:

cd navigate/to/the/folder
serve

And that’s it! A browser window will open, showing the index.html or a file list. Another feature is the auto-reload for for file changes. The approach is pretty naive (polling and creating a hash over file modification dates), but it works.

The full customizability is:

serve --address="localhost:3000" --folder="/my/folder" --auto-refresh="false" --open="false"

I open-sourced the tool on GitHub: github.com/xa17d/serve. You find also the binaries and more instructions there.

Background

I’m not a professional web developer, but I often play around with HTML, CSS, and JavaScript. To keep it simple, most of the time I don’t use any frameworks (like React,…) or toolchains (like NPM,…). The most basic way to test the HTML pages is opening them in the browser from the local file system. However, there are several limitations:

  • Cross-Origin Resource Sharing (CORS) restrictions: Browsers block access attempts to other files from file://. This makes sense from a security perspective, especially access attempts via JavaScript.
  • Cookies and Local Storage: May doesn’t behave as intended, also because of the Same origin policy.
  • Broken links: Links starting with / (e.g. /assets/style.css) don’t work as needed.
  • Security Warnings: JavaScript from a file:// URL often triggeres distracting security warnings.
  • Restricted JavaScript feature set, and more minor things.

All of this leads to big differences between development and production environment. And that’s why a simple webserver comes in handy, and also that it doesn’t depend on other toolchains. The auto-refresh helps with faster iterations.

If you’re in a similar situation as I am, give serve a shot and let me know what you think!

You might also like