Table of Content
- How to Set up a Node.js HTTP Server or Local Web Server Using http-server NPM Package
- Download and Install Node.js
- Install the http-server package from npm
- Start the Node.js web server
- Access the web pages served by the web server with a browser
- Wrap Off
This post is a quick guide on how to set up a simple HTTP web server on your local machine using Node.js.
The web server we create we run on the http-server NPM package.
The http-server
package is a simple and easy-configuration HTTP server that makes it easy to serve static files to the browser.
You can run it from the command line without creating a server.js file.
In this post, you will learn how to quickly set up a web server on your computer using Node.js and the http-server package.
How to Set up a Node.js HTTP Server or Local Web Server Using http-server NPM Package
Here are the steps you will take to set up a Node.js HTTP Server on a local development environment:
- Download and Install Node.js
- Install the http-server package from npm
- Start a web server from a directory containing static website files
- Access the web pages served by the web server with a browser
Download and Install Node.js
If you do not already have Node.js installed on your computer, download the latest stable release of Node.js from https://nodejs.org.
Select only the default options when installing.
Install the http-server package from npm
Install the http-server NPM package globally on your machine using the Node Package Manager (NPM) command line tool.
When you install the http-server package globally, you will be able to easily spin up a web server from anywhere on your computer.
To install the http-server package using npm
, open a terminal or command line window and enter the following:
npm install -g http-server
Start the Node.js web server
Using the terminal or command line, navigate to a directory containing your static web files such as HTML, JavaScript, CSS, etc.
For example:
cd /projects/ecommerce-raw-files
Run the command below to start the server:
http-server
You should get a similar output like below:
/projects/ecommerce-raw-files
Starting up http-server, serving ./
Available on:
http://192.168.0.5:8080
http://127.0.0.1:8080
Hit CTRL-C to stop the server
Access the web pages served by the web server with a browser
Open a browser tab and visit the address http://localhost:8080.
You should see your local website running.
Wrap Off
This is a short guide on how to quickly set up a web server on your computer using Node.js and http-server package.
Although most people are used to creating a server.js file, this process is simpler and comes in handy when you want to quickly set up a web server for testing or demonstration.
If you learned from this tutorial, or it helped you in any way, please consider sharing and subscribing to our newsletter.