http-server
Serve files over HTTP (simple webserver)
TLDR
Start an HTTP server listening on the default port to serve the current directory
Start an HTTP server on a specific port to serve a specific directory
Start an HTTP server using basic authentication
Start an HTTP server with directory listings disabled
Start an HTTPS server on the default port using the specified certificate
Start an HTTP server and include the client's IP address in the output logging
Start an HTTP server with CORS enabled by including the Access-Control-Allow-Origin: * header in all responses
Start an HTTP server with logging disabled
SYNOPSIS
http-server [path] [options]
path: Optional. The directory to serve. Defaults to the current working directory (.).
PARAMETERS
-p, --port
The port to listen on. Defaults to 8080.
-a, --addr
The address to bind to. Defaults to 0.0.0.0 (all available network interfaces).
-s, --silent
Suppress all output except for errors.
-d, --debug
Show more debug info.
-h, --help
Show help information and exit.
-v, --version
Show version number and exit.
-c, --cache
Set the Cache-Control max-age header. Defaults to 3600 seconds (1 hour). Use -1 for no-cache.
--cors []
Enable CORS via the Access-Control-Allow-Origin header. Optionally specify a custom header value, e.g., "*".
-S, --ssl
Enable HTTPS. Requires --key and --cert.
-K, --key
Path to the SSL key file.
-C, --cert
Path to the SSL certificate file.
-o, --open
Open the browser to the server URL once it's ready.
-i, --index
Specify the default index file name. Defaults to index.html.
-L, --no-dotfiles
Do not serve dotfiles (files starting with a period).
-E, --etags
Enable ETag generation.
-U, --utc
Use UTC time in logs.
-P, --proxy
Proxies all requests that are not found to the specified url.
-r, --robots
Respond to /robots.txt with "User-agent: *
Disallow: /". (This is a single line, formatted as HTML to represent multiline text)
-x, --ext
Default file extension for requests. E.g., --ext .html will serve foo.html for a request to /foo.
DESCRIPTION
The http-server command provides a rapid and easy way to serve static files over HTTP from the command line. It is a popular Node.js module, designed for local development, testing, and sharing files within a network without complex server configurations.
It automatically serves the current directory, or a specified path, making it ideal for front-end development, showcasing prototypes, or running single-page applications. Key features include support for CORS, caching, SSL, directory listing, and automatic opening of a browser upon startup. Its simplicity and portability (where Node.js is installed) make it a go-to tool for quick web serving needs, although it is not recommended for production environments due to its basic security and performance characteristics.
CAVEATS
- Node.js Dependency: The http-server command is a Node.js package and requires Node.js and npm (or yarn) to be installed on your system. It is not a native Linux binary.
- Development Use Only: It is designed for development and local testing. It lacks advanced features, security measures, and performance optimizations required for production-grade web servers.
- Static File Serving: Primarily serves static files. It does not execute server-side scripts (e.g., PHP, ASP.NET) directly.
INSTALLATION
To use http-server, you typically install it globally via npm or yarn:
npm install -g http-server
or
yarn global add http-server
BASIC USAGE EXAMPLE
To serve files from the current directory on port 8080:
http-server
To serve files from a specific directory on port 3000 and open in browser:
http-server ./my-project -p 3000 -o
HISTORY
The http-server module was created by Nodejitsu (now Npm, Inc.) and is maintained by the Node.js community. It gained popularity rapidly due to its ease of use and the growing ecosystem of Node.js and front-end development workflows. It serves as a classic example of a "batteries included" tool for quickly getting a local HTTP server up and running, fulfilling a common need for developers to test web content directly in a browser without complex setup.
SEE ALSO
python3 -m http.server: Python's built-in simple HTTP server, similar in functionality., nginx(8): A high-performance, production-ready web server and reverse proxy., apache2(8): A widely-used, robust, and feature-rich open-source HTTP server., serve: Another popular Node.js static file server, often used in conjunction with modern JavaScript frameworks.