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.
- To install it globally, run:
npm install --global http-server - Or you can run this from the directory with the files to serve:
npx http-server - 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
- Check that your static files are being served correctly by visiting the URL in a browser.
- Ensure that the terminal shows incoming requests as you interact with the served files.
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