http-serve: Command-line HTTP Server
raw JSON →http-serve is a command-line HTTP server that builds upon the foundational `http-server` package, enhancing it with key features such as gzip compression and a configurable fallback mechanism for nonexistent routes (404s). It also includes built-in HTTPS support, making it a robust solution for both local development and front-end testing environments. The current stable version is 1.0.1, indicating a mature yet focused tool. As a CLI utility, its release cadence is typically driven by new feature additions, bug fixes, or dependency updates. Its primary differentiators lie in its zero-configuration approach, ease of use for quick static file serving, and the added capabilities for gzipped assets and Single Page Application (SPA) routing compared to basic HTTP servers.
Common errors
error sh: http-serve: command not found ↓
npm i -g http-serve. Or run locally using npx: npx http-serve .. Or add a script to package.json and run with npm run <script-name>. error Error: listen EADDRINUSE: address already in use :::8080 ↓
-p flag, e.g., http-serve . -p 3000. Alternatively, identify and terminate the process currently using port 8080. error ERR_SSL_PROTOCOL_ERROR (in browser) or related HTTPS connection issues ↓
cert.pem and key.pem files are valid and correctly specified via --cert and --key. If using self-signed certificates for local development, you may need to manually trust them in your browser's security settings. error Files are not being served with gzip compression. ↓
app.js.gz for app.js). Use gzip -k to create them without deleting originals. Ensure your browser is configured to accept gzip encoding. Warnings
gotcha The `--gzip` option only serves pre-gzipped files. It does not compress files on the fly. You must provide `*.js.gz` for `*.js` etc., for it to work. ↓
gotcha The default serving path is `./public` if that directory exists; otherwise, it defaults to the current working directory (`./`). Be explicit with the `[path]` argument to avoid confusion. ↓
gotcha When using `--ssl`, you must provide paths to a valid SSL certificate (`--cert`) and key (`--key`) file. Using self-signed certificates for local development may trigger browser security warnings. ↓
gotcha `http-serve` is an 'extended version' of `http-server`. While it adds features like gzip and fallback, subtle behavioral differences, different default configurations, or divergence in unmentioned features might exist compared to the original `http-server`. ↓
Install
npm install http-serve yarn add http-serve pnpm add http-serve Imports
- http-serve wrong
import { serve } from 'http-serve'correctnpm i -g http-serve http-serve . -p 8080 - http-serve (via npx) wrong
node_modules/http-serve/bin/http-servecorrectnpx http-serve . -p 8080 --gzip - http-serve (package.json script) wrong
node http-serve.jscorrect// package.json { "scripts": { "serve": "http-serve ./dist -p 8081 --cors --fallback /index.html" } } // Then run: npm run serve
Quickstart
mkdir my-project
cd my-project
# 1. Initialize project and install http-serve
npm init -y
npm i -D http-serve
# 2. Create some static files
mkdir public
echo '<h1>Hello from http-serve!</h1><p>This is your index page.</p>' > public/index.html
echo 'console.log("This is an asset.");' > public/app.js
echo 'alert("404 Not Found!");' > public/404.html
# 3. Add a 'start' script to package.json
# (replace existing 'test' script or add to 'scripts' object)
npm pkg set scripts.start="http-serve ./public -p 8080 --ext html --gzip --fallback /404.html -o --silent"
# 4. Run the server
npm start
# This command starts an HTTP server on http://localhost:8080,
# serves content from './public', enables gzip (if .gz files exist),
# redirects 404s to /404.html, and opens the browser automatically.