Hello Node3 min read

Hello Node

I have written a post here that will give you a good idea on what Nodejs is all about. In this post I will walk you through the steps to install Node and run a Hello Node application.




  • Open Nodejs.org
  • Click on the green “Install” button
  • Depending on your OS a msi or a pkg will be downloaded
  • Run the downloaded file and leave all the options to default while running the setup
  • Thats it! You are done

PS: If you are interested, you can get the sources files from here and build your own version.


Open up a command prompt (in windows) or terminal (in *nix) and run node -v . This should present you with the version of Node you are running. Neat!

Hello Node

So lets say hello to Node. In the prompt, type  node and hit enter. This will take us to the Node world!!. This is know as  Node REPL (Read-Eval-Print-Loop), prompting you for a JavaScript line. Now type console.log("Hello Node!!");  You will get a reply  "Hello Node!!". Pretty easy right?

Lets write the same piece of code in a file and run it (just for fun). Create a new folder, lets call it node-dev  and create a new file inside it and name it helloNode.js.  Open this file in your favorite editor and add the below line of code to it.

console.log("Hello Node!!");

Now lets run it. Back to prompt [( Ctrl + C) twice]  in case the node server is still running from our previous example. And then navigate to the location where you have created the file  helloNode.js using  cd.

Now run node helloNode.js  Bam!! your message!!

Hello HTTP

Lets do something a bit more interesting. Lets write a simple HTTP server, that will dispatch a message.

The rule of thumb with Node.js is

Every function in Node.js is asynchronous.

Create a new file inside our node-dev folder and name it  server.js. Open this file in your favorite editor and add the below piece of code

Yes, that’s it. This is our HTTP server, that will understand all the HTTP requests and can respond accordingly.

Code Walk-through

Line 1: Node.js has a simple module loading system. In Node, libraries (in C) are called as modules. We can add our own modules (a post later on this). Here we are loading the HTTP module via the require()

Line 2: Lets specify the port we want our server to run on.

Line 4: Creates a new Server and this function takes a callback. Remember everything in Node is Async!. The anonymous function takes in the request and response object. In simple terms, Request is that data when the call is made to the server and Response is that data which leave from the server. Generally URL params e.g.  http://www.something.com?id=1 (the  id=1 part) are a part of the request object. And response can be anything from simple string to a complete page!. In this case it is a string “Hello HTTP!”.

Line 5: This will set a response header, describing the content type we are dispatching.

Line 6: This will write our message to the client.

Line 7: Unless the  response.end() is encountered, the response will not be dispatched back. This method will notify that the server operation is completed.

Line 10: We are initializing the server and asking it to listen to the port specified & then wait for things to happen.

Line 11: A friendly console log message

Now lets run this. Back to prompt then run node server.js. Open a browser and navigate to http://localhost:1234/  and there you can see your message!

Order of execution

On load of the js file, only lines 1, 2, 4, 10 &11 are executed. Then Node creates a new server and waits for something to happen. As soon as you open a browser and navigate to  http://localhost:1234/  a new event is raised notifying the event loop to dispatch lines 5,6,7 and then Node goes back to doing what it was doing..!

Hello TCP

You can create your own TCP  server using the  net package. Create a new file called helloTCP.js  and add the below code

You can run the above code in the prompt as node helloTCP.js. Open a browser and navigate to  http://localhost:1235/ and then come back to the prompt to see the output.

There are a lot of other thing that can be done with Node. I will unveil some in my future posts.

Thanks for reading! Do comment.