How To Create Simple Hello World Application In Node.js Step By Step

Description: In this post, we will learn about how to create a simple node js application that displays hello world in the browser. In this post, we will also learn how to create a server that runs on a port. See below step by step screenshot for creating the simple application using node js. If node js not install in your local system please see my previous post for how to install node js step by step. after installing node js in your local pc first check node js is install or not that try this example otherwise, this example is not working.

Step 1

First, open the command prompt by pressing Windows + R key and type cmd for opening the command prompt for running our sample application.
 
Node.js

Step 2

After opening the command prompt, create one node.js project folder in any drive for saving this example file. For example, I am using D: drive Tutorial folder. Inside this folder, create one more folder for saving the project. Here, we will create the FirstNodeExample folder.

Node.js

 

Step 3

Now, create one JavaScript file and name that app.js file inside FirstNodeExample folder. Now, open this file in any editor like Notepad, Notepad ++, Visual Studio. Here, I am using Visual Studio to write this code. Now, write the code in the app.js file and save that file.
 
Write the node.js code to display "Hello World!" in the browser.
  1. var http = require('http');  
  2. //create a server object:  
  3.   
  4. http.createServer(function (req, res) {  
  5.     res.write('Hello World!'); //write a response to the client  
  6.     res.end(); //end the response  
  7. }).listen(5000); //the server object listens on port 8080   
  8.   
  9. // Console will print the message  
  10. console.log('Server running at 5000'); 
 See the screenshot of how the code looks in app.js file.

Node.js

 Step 4

Now, again open your command prompt and type node app.js and press enter for running the node js file. See the below example screenshot.
 
Node.js

Step 5

Now, the Server is running. Here, we will create the server that is running on 5000 port for running this example. See the below screenshot for how to run this example. Just type localhost:5000 in your browser address bar and press Enter.

See the below output screenshot.

Node.js