When you're working on a site or web app, there will be times when you need to test files as if they were being served from a remote web server, similar to how they are when you browse the web normally.
If you don't have your own server, or just want to test files locally, you can set up a local web server on your computer to simulate one to do the job, and this tutorial is here to teach you how.
- Visit the official Node.js website to get the installer.
- After it downloads, run the installer until the end.
- Restart your computer to ensure the changes can take effect.
The Node.js installer should have also installed NPM for you. To confirm that you have installed both properly, you'll need to open Windows Command Prompt if you're on Windows, or Terminal if you're on Mac or Linux.
To check if you installed node:
To check if you installed NPM:
If both of these commands return a version number, you're good to go.
Now that we have Node and NPM installed, we can move forward and install the Node package called http-server, which is a simple zero-configuration HTTP server that serves up static files.
npm install http-server -g
This command will install it on your computer globally so you can use it anywhere.
Therefore, after you have navigated to your folder via the command line, start the server using this command:
If successful, you should see something similar to this:
http-server -o Starting up http-server, serving ./ Available on: http://192.168.1.8:8080 http://127.0.0.1:8080 Hit CTRL-C to stop the server
Upon seeing this, a browser window should have opened for you automatically, but if not, you can manually head to
http://127.0.0.1:8080 to see the folder that you used the command on being served.
Happy coding! 😄