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

Step 1

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


Step 2

After opening the command prompt, create one node.js project folder in any drive to save 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.


Step 3

Now, create one JavaScript file and name that app.js file inside the FirstNodeExample folder. Open this file in any editor like Notepad, Notepad ++, or 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.

var http = require('http');  
//create a server object:  
http.createServer(function (req, res) {  
    res.write('Hello World!'); //write a response to the client  
    res.end(); //end the response  
}).listen(5000); //the server object listens on port 8080   
// Console will print the message  
console.log('Server running at 5000'); 

 See the screenshot of how the code looks in the app.js file.


 Step 4

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


Step 5

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

See the below output screenshot.


Next Recommended Reading Running JavaScript Files in Node.js