JavaScript / nodejs

My First Node.js Application

This article is in continuation to the Nodejs Series. To refer how to install Nodejs on Ubuntu follow the link Setting up Nodejs Environment on Ubuntu

Hello world application that returns “Hello World” to any HTTP requests.

Move to your projects directory

user@LT-201: mkdir /projects/trials/nodejstrials

create a helloworld.js using the vi editor

user@LT-201:vi helloworld.js

Write the code to return a Hello World string with a 200 Status Response

var http=require(‘http’);
http.createServer( function (req,res) {
res.writeHead(200,{‘Content-type’: ‘text/plain’});
res.end(‘Hello World\n’);
}).listen(8080, ‘’);
console.log(‘Server running at’);

The nodejs application simply listens to a particular port on a given IP Addreess. On receiving a request on that IP Address and Port, it simply returns the response Hello World with a 200 status code.

user@LT-201:~/projects/trials/nodejstrials$ node -v

To test the application which has been written issue the following node command on the app server. The command starts the Nodejs Server at the configured IP Address and Port #
user@LT-201:~/projects/trials/nodejstrials$ node hello.js
Server running at

While the app server is running, we cannot issue any new node commands until we break it using CTRL + C

To test the application from a web server, issue a curl command from a new terminal

user@LT-201:~$ curl
Hello World

In case if you stop the nodejs application and then issue a curl command in the other terminal

user@LT-201:~$ curl
curl: (7) Failed to connect to port 8080: Connection refused

In the next article we would see to how to configure NPM

Leave a Reply

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