Introduction to node.js

I guess everybody in the field heard about node.js already, but likely most of them have ignored it, just like i did, questioning its future without getting into its details. Well, i do know now that i have made a mistake by doing so. Lately, i have gone through node.js documentations and list of addons, and i have to say it: it's incredibly awesome! And here is why. Basically, what node.js has to offer is a hole new horizon for what network applications are. Its creator, Ryan Dahl, needed a to create web sites with push capabilities as seen in web applications like Gmail. after several trials, he has chosen JavaScript because it didn't have an IO api, so he could have the freedom in defining one as he wishes.

[caption id="" align="alignright" width="635"]Ryan Dahl Ryan Dahl[/caption]

He started developing it with some main points in mind:

JavaScript would obviously seem to be the best option to create an event driven technology. the fact that node.js is event driven, and that it has a non blocking IO is pretty connected, the event mechanism is used to make callbacks everywhere, so a basic file read instruction for instance, can be non blocking by setting what to do with the data grabbed from the file as a callback function, so that while the system is reading the file, it can continue executing the script and serve the data when it comes. Take thin example:

var fs = require('fs');
var contents = fs.readFileSync('users','utf8');
console.log(contents);
console.log("Hello Node\n");
var contents = fs.readFileSync('hosts','utf8');
console.log(contents);
console.log("Hello again!");

this code is blocking, means every instruction is executed only if its predecessor  is fully executed. This approach is what believed to be non network friendly, the node.js way to do it is:

var fs = require('fs');
var contents = fs.readFile('./users','utf8', function(err,contents){
     console.log(contents);
});
console.log("Hello Node\n");
var contents = fs.readFile('./hosts','utf8', function(err,contents){
     console.log(contents);
});
console.log("Hello again!");

You have to know that, when you try this code, you will get an unexpected result, the 2 instructions that do not depend on the file system read call are executed in the first place,

Hello Node
Hello again!
list of users...
list of hosts...

this weird order comes from that the list of users and the list of hosts have to be read first, so that would take time, so we printed them in the callback of the read function, in other words, read the file, once done echo it, but meanwhile, do something else.

Best part of node.js is that it doesn't rely on an external software to do the web server part, it's the web server, the website and everything in the same time! that allows to build fast, scalable and flexible web applications with minimum overhead.

But what about performance? node.js is built on top of Google's V8 JavaScript engine, the libuv platform abstraction layer, which are written in C++ and C, the world fastest programming languages. That would have huge effect on the time and resources needed in order for node.js to interpret the code. I don't think there would be a better solution.

Conclusion

node.js is really useful and it has a great future. It's easy to learn and everyone should at least know about it. I'll be posting tutorials and open source projects I have written using node.js, so follow me and stay tuned! Meanwhile take a look at node.js website.

 

blog comments powered by Disqus