Web server

How to install and use the Node.js http server (web server) via NPM

Apache and Nginx are two popular web servers that are used to install various types of web applications. However, if Node.js is installed on your system, you can use a lightweight command line HTTP server through the NPM package manager. It’s not like Apache and Nginx but rather static file server on browser without creating server.js file. Just switch to the directory with HTML pages and start the installed HTTP server. For the tutorial, we are using Ubuntu 19.04, however, the commands will be the same for earlier versions of Ubuntu and Windows / MacOS.

Installation of the Http Node.js server + example

Step 1: Install Node.js and NPM on your Windows or Linux operating system.

The simple commands to install Node and NPM on Ubuntu are:

sudo apt-get install nodejs

sudo apt-get install npm

For the detailed installation tutorial, see:

Step 2: Install the http server using the NPM package manager

Open Command Prompt / Power Shell in Windows or Command Terminal in Ubuntu and run the following command to install and configure a simple NodeJS -HTTP server / local web server.

 npm install -g http-server

At Ubuntu use the above command with sudo.

Step 3: Check whether the http server is working or not

To check whether our installed Node JS HTTP web server is working or not, use the following command

http-server

The above command will display the IP address where the Local Node Js web server is running and can be accessed.

The port number depends on which one is free and available. If you want to use a specific port number, use the -p syntax after the above command. For example-

To use port number 8080, the command will be:

http-server -p 8082

It will start the http server under the port 8082.

Step 4: Go to your installed http server

Go to the browser and point to the IP address given by the http-server command on the terminal or the command prompt.

local naodjs http server

Step 5: Use a Custom Web Project or Static HTML Files

If you want to use your own web project with Node JS web server, just change to this directory first, then type the command http-server,

Example of http server:

I downloaded a free HTML template from the internet which is in my Downloads case. So first I switched to Downloads then at the case which contains the HTML template. After pulling the command http server.

Personal http node js server project

This time when I opened the same local IP the pattern would have appeared.


Source link

Leave a Reply

Your email address will not be published. Required fields are marked *