How to use the command 'http-server' (with examples)

How to use the command 'http-server' (with examples)

The ‘http-server’ command is a simple static HTTP server that allows you to serve static files. It is a convenient tool for quickly hosting a website or sharing files over a network.

Use case 1: Start an HTTP server listening on the default port to serve the current directory

Code:

http-server

Motivation: This use case is useful when you want to quickly set up a local server to serve the files in the current directory. It can be handy for web development or sharing files with others.

Explanation:

  • The ‘http-server’ command starts the HTTP server.
  • Without any additional arguments, it listens on the default port (usually 8080) and serves the files in the current directory.

Example output:

Starting up http-server, serving ./
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Use case 2: Start an HTTP server on a specific port to serve a specific directory

Code:

http-server path/to/directory --port port

Motivation: This use case is helpful when you want to serve files from a specific directory instead of the current directory. Additionally, you can specify a custom port to use for the server.

Explanation:

  • The ‘path/to/directory’ argument specifies the directory to serve files from.
  • The ‘–port’ option followed by ‘port’ sets a custom port for the server to listen on.

Example output:

Starting up http-server, serving path/to/directory
Available on:
  http://127.0.0.1:8888
  http://192.168.0.10:8888
Hit CTRL-C to stop the server

Use case 3: Start an HTTP server using basic authentication

Code:

http-server --username username --password password

Motivation: This use case is valuable when you need to restrict access to your server by implementing basic authentication. It adds a layer of security to prevent unauthorized access.

Explanation:

  • The ‘–username’ option followed by ‘username’ sets the username for basic authentication.
  • The ‘–password’ option followed by ‘password’ sets the password for basic authentication.

Example output:

Starting up http-server, serving ./
Basic authentication enabled: username:password
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Use case 4: Start an HTTP server with directory listings disabled

Code:

http-server -d false

Motivation: This use case is useful when you want to prevent directory listing on your server. Disabling directory listings ensures that visitors cannot see the files present in a directory.

Explanation:

  • The ‘-d’ option followed by ‘false’ disables directory listings.

Example output:

Starting up http-server, serving ./
Directory listings are disabled.
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Use case 5: 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: This use case is beneficial when you want to serve your website or files over HTTPS for secure communication. You can provide your custom SSL certificate and key for the server.

Explanation:

  • The ‘–ssl’ option enables HTTPS support.
  • The ‘–cert’ option followed by ‘path/to/cert.pem’ sets the path to the SSL certificate file.
  • The ‘–key’ option followed by ‘path/to/key.pem’ sets the path to the SSL key file.

Example output:

Starting up http-server, serving ./
HTTPS server listening on port 443
Available on:
  https://127.0.0.1
  https://192.168.0.10
Hit CTRL-C to stop the server

Use case 6: Start an HTTP server and include the client’s IP address in the output logging

Code:

http-server --log-ip

Motivation: This use case is useful when you need to track the IP addresses of clients accessing your server. It helps in monitoring and analyzing the server’s usage.

Explanation:

  • The ‘–log-ip’ option enables logging of the client’s IP address in the server’s output.

Example output:

Starting up http-server, serving ./
Including client IP in logging.
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Use case 7: Start an HTTP server with CORS enabled by including the Access-Control-Allow-Origin: * header in all responses

Code:

http-server --cors

Motivation: This use case is beneficial when you want to enable Cross-Origin Resource Sharing (CORS) on your server. It allows web applications from different domains to make requests to your server.

Explanation:

  • The ‘–cors’ option enables CORS support by including the Access-Control-Allow-Origin: * header in all responses.

Example output:

Starting up http-server, serving ./
CORS support enabled.
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Use case 8: Start an HTTP server with logging disabled

Code:

http-server --silent

Motivation: This use case is useful when you want to run the server without any logging output. It can be handy in scenarios where you only need the server to function silently without any unnecessary information.

Explanation:

  • The ‘–silent’ option disables logging output from the server.

Example output:

Starting up http-server, serving ./
Logging disabled.
Available on:
  http://127.0.0.1:8080
  http://192.168.0.10:8080
Hit CTRL-C to stop the server

Conclusion:

The ‘http-server’ command provides a simple and flexible way to host static files over HTTP or HTTPS. By leveraging its various options and arguments, you can customize the server’s behavior according to your specific requirements. Whether it’s for development, file sharing, or serving websites, ‘http-server’ is a handy tool to have in your toolkit.

Related Posts

Understanding the "jobs" Command (with examples)

Understanding the "jobs" Command (with examples)

The “jobs” command is a shell builtin command that is used to view information about processes spawned by the current shell.

Read More
How to use the command 'tye' (with examples)

How to use the command 'tye' (with examples)

The ’tye’ command is a developer tool that aims to make developing, testing, and deploying microservices and distributed applications easier.

Read More
How to use the command 'needrestart' (with examples)

How to use the command 'needrestart' (with examples)

The ’needrestart’ command is a useful tool that allows you to check which daemons need to be restarted after library upgrades.

Read More