Skip to main content

Simple Web Server Using NodeJs and ExpressJs

Today , I am going to show you how to create a simple web server using node.js and express.js. If you are new about nodejs then read about nodejs on . You can create a web server with nodejs using few lines of code. Just to go nodejs home page and download the latest version of nodejs and install on your machine [windows, mac or Linux]. I have installed nodejs on my windows machine. To check whether nodejs has been installed successfully in your machine , go to command prompt and run command node -v it will print current version of nodejs if installed successfully.

we have completed the first step, installed nodejs in our machine. Now I will show you the power of nodejs , just copy these line and create a new file name as server.js  and paste the code in that file and save it.

Now , open your command terminal and go to this file path and run command

nodejs server running on localhost
nodejs server running on localhost

This will print on your console, server running at , it means basic http server start running if you open this url in your browser it will print hello world in browser screen.

hello world example of nodejs
hello world example of nodejs

That’s it , your simple nodejs server is running and now you can put files under js folder and change nodejs server  to send static files too but making change without any framework is difficult same as handling DOM element with vanilla JavaScript, to make life easy ExpressJS is very popular nodejs framework that will make your life easy. If you are not aware about expressjs then you can read about it here. Before installing expressjs , install npm (node package manager) in your system. just check that npm is already installed in your system along with nodejs , open command prompt and run command npm -v , this is print current version.

Now with help of npm you can install expressjs in your working directory , go that project directory and run command

Now, go to directory and create a new file name index.js and paste the following code.

go to terminal and run

this will print on your console, "Example app listening on port 4000!", now go to browser and hit http://localhost:4000 and voila , you will get "hello world" as response.

so if you see , by using Expressjs  framework its become easy to write and understanding but internally Expressjs using nodejs api, its just give as a wrapper api in simple and understandable way.
Its just a example but when your start using node in your project you need routing , handling static content then using node API become hectic because its not straight forward , its same as doing DOM manipulation using native JavaScript. Expressjs has lots of simple api to use, you can go through expressjs documentation 

One thought on “Simple Web Server Using NodeJs and ExpressJs

Leave a Reply

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