main advantages and usage of node.js
node.js main advantage is that it doesn’t have any flaws that usually appear when we work with streams – creation of new data structures providing stream work, blocking memory, etc. for example, in apache web-server for every connection the new stream is created with initial data of 2 mib.
taking into account the specific features of node.js, it is mainly used for development of next types of programs:
– programs executing one task in one launch, so-called scripts.
– executable applications, such as different types of servers. but in such application a developer should apply to some rules in order to provide high capacity. all actions should be smaller in order to keep high level of capacity, and also we should check that the application isn’t overloaded by memory consumption.
node.js usage example: message server based on socket.io
now we will have a look at the example of node.js application realization using library socket.io , that will execute functions of message server.
first of all, you should remember that you should use node.js in development with a modular approach, so that code is not only readable, but also easy to maintain, extensible and would be able to be used for various purposes, if necessary (example of such module you will see further is listing chatserver.js). then the code to run our server may look like this:
var chatserver = require('./lib/chatserver').chatserver(443); chatserver.start();
– require(‘./lib/chatserver’) returns link to module (module is js file with the name, in our case, chatserver, that provides external methods). it also reveals one method for external usage – chatserver(443) constructor with the port parameter that server should “listen to”.
we used class and module approaches together with programming patterns in the development of example above. as you can see, such code is readable, easy to modify and portable.
from one side, in order to write applications using node.js you don’t need to have high level qualification. but, from my experience, if you don’t use such approaches your application will turn into the long code sheet, that is difficult to read and to support, and it is easier to rewrite it if you need to modify it than to try understanding the original code.
development process using node.js has its advantages and disadvantages as any other technology. such qualities altogether allow us to solve certain tasks. so to answer the question, “whether to use node.js or not?” you need to evaluate whether node.js conforms to the requirements of the project, as well as compare it with others analogs, using such factors as technical specifications or technology development prospects.
in conclusion i’d like to say that neither technology is holy grail, vice-versa, every technology is supposed to solve some specific list of tasks.