How to Use the Command 'http-server' (with Examples)
The http-server
is a useful utility for serving static files in a directory over HTTP. It allows developers to quickly spin up a web server to test or display web content without extensive setup. Whether you’re conducting quick tests or showcasing a static website, http-server
offers a straightforward way to do so. It supports various options tailored for development needs, from securing connections to enabling cross-origin resource sharing.
Start an HTTP Server Listening on the Default Port to Serve the Current Directory
Code:
http-server
Motivation:
This basic usage of http-server
is ideal for someone who wants to quickly serve a static website or files from their current directory. It’s perfect for developers who need to test HTML, JavaScript, or other web resources without leaving their terminal.
Explanation:
http-server
: This initializes the server in the current directory. By default, it serves the content over HTTP on port 8080.
Example Output:
Starting up http-server, serving ./ on: http://0.0.0.0:8080
Start an HTTP Server on a Specific Port to Serve a Specific Directory
Code:
http-server path/to/directory --port 8081
Motivation:
You might want to serve files from a specific directory at a particular port to avoid interference with other services. This is particularly useful in scenarios involving multiple projects running on different ports.
Explanation:
path/to/directory
: The path where your files reside, which you intend to serve.--port 8081
: Sets the server to listen on port 8081 instead of the default.
Example Output:
Starting up http-server, serving path/to/directory on: http://0.0.0.0:8081
Start an HTTP Server Using Basic Authentication
Code:
http-server --username admin --password secretpassword
Motivation:
When you need to restrict access to the served files, enabling basic authentication can be an effective way to require a username and password upon attempting to access the content.
Explanation:
--username admin
: Specifies the username required to access the server.--password secretpassword
: Specifies the password necessary for access.
Example Output:
Starting up http-server with authentication, serving ./ on: http://0.0.0.0:8080
Start an HTTP Server with Directory Listings Disabled
Code:
http-server -d false
Motivation:
If you want to hide the directory listing when users access your server, this option prevents the exposure of all files in the directory, allowing only direct link access to known resources.
Explanation:
-d false
: Disables directory listings, so users cannot see a list of files within the directory by visiting the server’s root URL.
Example Output:
Starting up http-server with directory listing disabled, serving ./ on: http://0.0.0.0:8080
Start an HTTPS Server on the Default Port Using the Specified Certificate
Code:
http-server --ssl --cert path/to/cert.pem --key path/to/key.pem
Motivation:
Serving content over HTTPS is crucial for security, protecting data integrity and confidentiality, especially when demonstrating an application that relies on secure connections, like a login page.
Explanation:
--ssl
: Enables HTTPS serving.--cert path/to/cert.pem
: Specifies the path to the SSL certificate file.--key path/to/key.pem
: Specifies the path to the SSL key file.
Example Output:
Starting up https-server, serving ./ on: https://0.0.0.0:8080
Start an HTTP Server and Include the Client’s IP Address in the Output Logging
Code:
http-server --log-ip
Motivation:
In development and testing environments, logging client IPs can help monitor where requests are coming from, which can be critical for debugging network-related issues or understanding usage patterns.
Explanation:
--log-ip
: Includes the client’s IP address in the server’s log output.
Example Output:
http-server logging requests with client IP addresses, starting at http://0.0.0.0:8080
Start an HTTP Server with CORS Enabled
Code:
http-server --cors
Motivation:
Enabling Cross-Origin Resource Sharing (CORS) allows your resources to be accessible from other domains, facilitating the integration of web services and APIs by removing the same-origin policy restrictions.
Explanation:
--cors
: Adds theAccess-Control-Allow-Origin: *
header to the server’s responses, enabling resource sharing across origins.
Example Output:
Starting up http-server with CORS enabled, serving ./ on: http://0.0.0.0:8080
Start an HTTP Server with Logging Disabled
Code:
http-server --silent
Motivation:
In scenarios where you want a clean terminal without logs cluttering the display, perhaps during a demo or performance testing, disabling logging can simplify command line output.
Explanation:
--silent
: Suppresses all output logging to the terminal, ensuring a clean interface.
Example Output:
No output, as logging is disabled.
Conclusion:
The http-server
utility provides a versatile set of options to cater to various needs of serving static files over HTTP/HTTPS. From enabling security measures to customizing server behavior through options like CORS and logging, http-server
equips developers with a powerful tool for local and small-scale web serving tasks.