Hello Node

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page

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.

Contents

Setup

Installation

  • 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.

Validation

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.
@arvindr21

Tweet about this on TwitterShare on LinkedIn0Share on Google+0Share on Reddit0Buffer this pageFlattr the authorEmail this to someonePrint this page
  • Tech4Lead

    Great explanation !!! Thanks

  • 8ne

    Hi, nice tutorials.

    I tested HelloHTTP and works perfectly, now I tested helloTCP but once it starts I can not connect the navigator says “ERR_CONNECTION_REFUSED” . I dont know how to correct, any suggestion?

    • 8ene

      I discover the problem, I was doing the test remotely on my raspberry Pi and this is not localhost. I change “locahost” by “0.0.0.0”. Now it works remotely.

      • http://thejackalofjavascript.com/ Arvind Ravulavaru

        Great!

  • http://ideshibasara.blogspot.in/ Vikrant Singh

    Thanks Arvind. Really interesting web stuff for a beginner.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Vikranth. Glad it helped.

  • Randy Davis

    Arvind, I’m hooked on your style of doing things! Keep it up as you have gained a follower for a long time to come! Great stuff!

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Randy. Glad I could be of help.

  • Tara Prasad Dash

    Great place to start. Thanks a lot.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Tara!

  • Jeff

    Thanks Arvind for all of your tutorials. They are eloquently written and easy to follow. Cheers.

    • http://thejackalofjavascript.com/ Arvind Ravulavaru

      Thanks Jeff!