All about programming in GNU/LINUX

NODE.JS:Introduction……Writing servers at will…

Have you ever come across a language which is mostly dedicated for writing servers?? we’ll here is one using which you can write servers at will and in a very efficient way too.Node brings in a easy bridge between the system and the web.Node is based on Java Script and google;s V8 Javascript engine

Comparative Advantages of writing servers using node

  • First , the language used for writing Node programs , The Java Sript,this itself makes programmers life very much easier
  • Writing servers using any other programming language is tedious task since multiple access and concurrency mechanisms are to be explicitly taken care of .Spawning a new process or a thread for servicing every new request have to be explicitly code in any programming language , but these things are implicitly taken of by the node since the language itself is particularly dedicated for writing servers.
  • The client gets access to the services of the Node server using their browser
  • Easily write servers for any purpose:From displaying content of a text to the clients to chatting over web page , any resources from the system can be easily utilized to provide services to the clients over the browser
  • The asynchronous programming model:This is a new efficient approach in handling client requests , Node doesnt spawn new processes or threads for catering every clients request , it handles everything in a single thread instance with a new asynchronous approach.

  • Its all about using a module and writing a callback function:Writing codes in NODE is all about loading a module , calling their functions and setting up an callback function on occurance of an event.here is the link of documentation for all node modules Node modules documentation
  • This is how a simple hello world server in nodeJs looks like

    //load http module 
    var http=require('http');
    //create http server 
    http.createServer(function(req,res) {
      	//content header
    	res.writeHead(200,{'content-type':'text/plain'});
    	//write message and signal communication is complete
    	res.write("hello,world!\n");
    }).listen(8124);
    console.log('Server running on 8124');
    

    Here is a node server which reads a file from the disk and displays it for any client who access “serverip:8124” from their browser.The code uses the fs and http modules of the node

    //load http module 
    var http=require('http');
    var fs=require('fs');
    //create http server 
    http.createServer( function(req,res)  {
    fs.readFile('firstnodeserver.js','utf8',function(err,data) {
    	res.writeHead(200,{'Content-type':'text/plain'});
    	if(err)
    		res.write('Could not open the file\n');
    	else
    		//if no error write the file to cilent 
    		res.write(data);
    	res.end();
    	});
    }).listen(8124,function() { console.log('bound to port 8124');});
    console.log('Server running on 8124');
    
    Advertisements

One response

  1. Hello my loved one! I want to say that this article is awesome, great written and come with approximately all vital infos.
    I would like to peer more posts like this .

    May 20, 2014 at 5:32 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s