Create a Simple HTTP Server with NodeJS

View more Tutorials:

1- The objective of post

In this post, I am going to show you how to create a very simple HTTP Server with NodeJS. And you can access static data sources such as HTML, Javascript, CSS, etc in the directory via http, for example:
  • http://localhost:8080/abc.html
  • http://localhost:8080/abc.js
Instead of accessing through ​​​​​ file:///
  • file:///E:/webexamples/reactjs/abc.html
  • file:///E:/webexamples/reactjs/abc.js
The question is why do you need a  HTTP only to access static data sources?
If you want to learn ReactJS you will need an HTTP. Although ReactJS is just a Javascript library, and HTML files can be run directly in a browser. However, sometimes  browser prevents the operations of the local Javascript files. It requires the Javascript file to come from one of the schemas: http, data, chrome, chrome-extension, https.
Error Message!
Failed to load file:///E:/webexamples/reactjs/hello-reactjs/person.jsx:
Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.

2- Create NodeJS HTTP Server

First of all, make sure that you have installed the NodeJS on your computer. Otherwise, please install it as in the following instructions :
Create an empty directory:
Open the CMD window, and CD to the folder you created in the previous step.
Run the following command to install http-server:
npm install -g http-server
Start  HTTP-Server:
# Note: -c-1 to disable caching!!

http-server -c-1
Your HTTP Server has been started.  it is listening at port 8080. You can check it by visiting the following link:
Create a HTML file in that directory, and you can access it via HTTP:

View more Tutorials: