Sunday, February 23, 2014

Getting Started with Node JS

So, you want to learn NodeJS, huh? Apparently, there could be no other reason of why you have landed on this very post. Well, in that case actually, you are at the right place. Be with me for sometime and we would be exploring NodeJS on top of everything.
Unlike various other server side programing methods that exists in the programming realm, NodeJS is one of them. It’s not new however, the server side JavaScript, but with NodeJS, JavaScript has become one of the most elegant and appealing server framework. In this post and other followed ones, we would be learning on how this NodeJS stuff works and would deep dive into the realm of server side JavaScript framework.

What is NodeJS?

Right from the beginning, JavaScript was viewed as the client side language, that executes on the browser and do the stuffs like manipulating DOM etc. But from the last few years, the realm of JavaScript has increased potentially. It is no longer bound just within the naïve of a web browser, NodeJS makes it all possible. NodeJS is basically a framework that enables JavaScript to work at the server side and do server stuffs.
Node takes the basic of the language, add different APIs on top of it to ensure writing of code that’s meant to power the network related tasks.
Things will get clear when we dive into coding. So let’s took a dive into how exactly we can do stuffs in NodeJS.

Hello NodeJS

For doing server operations, we need a server. A server is basically a program that scans the incoming requests, coming from other programs and respond them accordingly. For .Net we have IIS, for PHP we have Apache and the list would go on. NodeJS however, do not require a separate server, in contrast of that, NodeJS provide us modules to create our own HTTP/TCP servers. Here is a simple NodeJS program that starts a HTTP server, listening at port 8080.

image

Here are the steps that are followed in the code above.

1. First off a module ‘http’ is imported. NodeJS comes with lot of different modules which serve different purpose. Importing ‘http’ module would allow us to create a HTTP server that would listen to incoming HTTP requests.

2. Line number 4 is where we are actually creating a server by calling a method ‘createServer()’. This method takes an argument, which is basically a callback function, that executes when a request to the server is made.

3. listen(<port>), actually starts the server at the port number specified.

Above code is very uninteresting actually. It is just listening to the incoming requests, but is not responding back to the caller with whatsoever. Let’s make the code a bit more interesting, by responding the caller with a ‘Hello Caller’ message.

   1: var http = require('http');
   2:  
   3: http.createServer(onServerRequested).listen(8180);
   4:  
   5: function onServerRequested(request, response){
   6:     response.writeHead(200);
   7:     response.write("Hello Caller");
   8:     response.end();    
   9: }


Adding these 3 lines inside the function onServerRequested() would suffice. The line #8 indicates that the call was a success, 200 is the OK code for HTTP request. Line #9 reply the caller with a message ‘Hello Caller’ via the same channel from where the request came. Line #10 closes the connection.

Execute the code from the command prompt like:

image

And requesting the server via any Web Browser would result in:

image

Hope this post would have served the basic understanding of NodeJS. In the future posts we will be looking at some other stuffs that can be done using NodeJS.

Happy Coding!!!

Share this post

0 comments

:) :-) :)) =)) :( :-( :(( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ :-$ (b) (f) x-) (k) (h) (c) cheer

 
© 2013 Neelesh Vishwakarma
Posts RSS Comments RSS
Back to top