http-server from npm

A short guide on how to use the http-server package from npm to serve static files.

1) Installation and why http-server

To install and use the http-server package from npm, you can use the npx command to run it instantly without full installation, or you can run npm install --global http-server to make it available anywhere on your machine. This zero-configuration tool allows you to instantly spin up a local web server to serve static files like HTML, CSS, and JavaScript files.

  1. To install it globally, run:
    npm install --global http-server
  2. Or you can run this from the directory with the files to serve:
    npx http-server
  3. Prerequisites for skipping installation: You need to have Node.js and npm installed and be in the directory containing the files you want to serve.

2) Validation

To verify that http-server is working, the terminal will output local web addresses that you can launch and see your files. You can open a web browser and navigate to the address shown in the terminal, typically similar to this:

Available on:
        http://127.0.0.1:8080
        http://192.168.1.5:8080
        http://192.168.1.5:8080
        Hit CTRL-C to stop the server

3) Troubleshooting

Common issues may include port conflicts or missing files. Ensure that the port http-server is trying to use is available and that the files you want to serve exist in the directory.

npx http-server -p 8080
Feel free to contact us if you have any issues or further questions. You can also take a look at our guide here