Sunday, May 24, 2015

What is Node.js ?

Node.js is a platform built on Chrome’s Javascript runtime for easily building fast, scalable network applications. Node.js uses an event driven, non blocking I/O model that makes it lightweight and efficient, perfect for data intensive real-time applications that run across distributed devices. 

Excited by the features, as per the documentation, below is the only lines of code that to be written to create a web server it seems! 

var http = require (‘http’);
http.createServer(function(req,res))
{
res.writeHead(200,{‘Content-Type’ : ‘text/plain’});
res.end(‘Hello World!’);
}).listen(1337,’127.0.0.1’);
console.log(‘Server running!’);

To run the server, one need to put the code into a file example.js and execute it with the node program from command line. 

%node example.js 

If we want to create a socket server, that is also easy. 

var net = require(‘net’);

var server = net.createServer(function(socket){
socket.write(‘Echo server\r\n’);
socket.pipe(socket);
}

Still need to get real hands on this, this is just a beginning of exploration.

References:

No comments:

Post a Comment