Node.js Hello world code

Let me write the program and will explain line by line.

http = require(‘http’)
    res.writeHead(200, {‘content-Type’:’text/html’});
    res.write(“<h1>Hello Word!</h1>”)
    res.end(“<h2>This is the end!</h2>”);
}).listen(3000, “”);
console.log(“Server running at;)

Let’s create a new file called hello_world.js.

Now Node comes with a handfull of libraries to make the development of event-based servers easy.

To use one of the available libraries, We simply include its module using the require function.

http_object = require(‘http’)

The require function will return an object representing the module that you pass into it and you can capture that object in a variable.


For running app, we need to create HTTP server.  Node provides the http library. With reference to http_object create server.


The createServer that takes a callback function and returns a new server object.

The callback function is what Node calls a listener function and it is called by the server whenever a new request comes in.

Whenever an HTTP request is made, the listener function will be called and objects representing the HTTP request and response will be passed into the function.


Then first need to write the appropriate HTTP headers.

res.writeHead(200, {‘content-Type’:’text/html’});

The writeHead function takes a couple of arguments. The first is an integer value representing the status code of the request which for us will be 200, in other words,

Then we’ll call the write function and pass in the data that you wish to send.

 res.write(“<h1>Hello Word!</h1>”)

Now that we’ve created our server, we need to set it up to listen for new requests.

listen(3000, “”);

The listen function on our server object and pass in a port number for it to listen on(3000). The listen function also takes an optional second parameter which is the hostname URL(“”),

Finally, let’s print out a message to let us know that our server is running

console.log(“Listening on“).

There we go, now let’s run our app by calling node and passing to it the name of the file we want it to execute.

node hello_world.js and open browser “

That’s it. Enjoy………………..

Install Node.js on Ubuntu System

Let’s take a quick look to install node.js on UBUNTU system.

1. Download node.js from

Then open terminal and go to download folder and extract node.tag file

2. tar xvf .node-v0.6.17.tag.gz

3. Now install libssl-dev

sudo apt-get install libssl-dev

4. Move to extracted node-v0.6.17 folder

cd node-v0.6.17

5. then fire ./configure command from console.


Will do a lot of configuration setting for us.

6. Type make and post sudo install make

7. Make sure node and npm had got install (We not need to install npm seperatly, it’s build in node.js itself)

node -v

npm  -v

That’s it. Enjoy with node.js

Hopefully in next post will write Hello world program.


  • It´s web server is able to handle a HUGE number of connections out of the box
  • Various libraries can be run on browser, the same as in the server
  • Very friendly to Websockets (real-time web apps)
  • Lots of libraries are being ported to it from other langs.
  • Express, inspired in ruby´s Sinatra; is very light on memory but also very powerful