Creating a static server using nodejs

I came across a scenario where you want to create a static web site in your localhost. There are various ways to accomplish that. The one I was very familiar is to paste your web site in web app folder of your apache-tomcat.

Now I show you, how to create  simple  static web  server using nodejs.

First Way: using http-server
Run these lines in your terminal and you are done.

Now go to your browser and hit http://localhost:8888/index.html if index.html exists in MyApp folder. If you want to know more about http-server, you can find the documentation here.

Second Way: using connect and server-static
For that you need to install “connect” and “serve-static” npm packages in your specific folder. So I created a folder staticserver and then executing the below command to install the following packages.

After installation you can see these packages inside your node_modules folder.

Now create your js file to start server and name it as server.js

Create a sample html file or group of file to access it using the hosting url.
For example, I created a sample index.html file.

Now its turn to execute your server.js

So if no error appears on the command prompt, you can access the index.html by hitting the below url

The Way I like the most: using live-server node module

Go to the folder where you have your index.html file and run

Facebooktwittergoogle_plusredditpinterestlinkedinmail

One thought on “Creating a static server using nodejs

  1. Pingback: writeaessay

Leave a Reply

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