Published on: 06 Aug, 2019

How to create Hello World Http Server in Node.js

Tumblr Digg Google Plus

In this tutorial, you will learn how to create a Hello World Http Server in Node.js. If you are new to Node.js, you probably want to read the previous article on how to install Node.js on Windows. You can find out more details by clicking this link. You can also watch the video tutorial on our youtube video channel.

The first thing we need to do is to create a Javascript file. Let's name it helloworld.js. Then we copy and paste the following Javascript code.

var objHttp = require('http');
var objServer = objHttp.createServer();
objServer.on('request',
	function(request, response){
		response.writeHead(200, {'content-type': 'text/plain'});
		response.write('Hello World');
		response.end();
	}
);

var serverPort = 8000;
objServer.listen(serverPort);
objServer.once('listening', 
	function(){
		console.log("Listening on port " + serverPort);
	}
);

The next step is to launch the Node.js server. Open the Node.js command prompt and launch it by typing in the following code.

Node helloworld.js

If there is nothing wrong, you should see a console log on your command prompt saying "Listening on port 8000".

You can now connect to your Node.js by opening the following URL in your web browser.

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.