What is Node.js? Why is it Important? What are the Benefits?

RobOwner (Aidellio)
I'm an enthusiastic IT Business Solutions Provider that designs and creates a solution YOU want for you and your customers

What is Node.js?

Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event driven, which throws a whole new light on the way a server side app performs. As Node.js is the complete package (web HTTP server and server side language), there is dramatically less overhead in implementing it as a webserver when compared with a typical Apache (and PHP) or IIS (with ASP/PHP) installation.

Node.js is written in JavaScript, the most common client-side language around. This reduces the learning curve somewhat for most web developers as they understand JavaScript and how to code it.

Why is it Important and what are the Benefits?

Most of the server side languages I’ve used are built on sequential execution models, meaning one task has to finish before another one begins. For instance, PHP (in conjunction with the type of server it is running off e.g. Apache) will spawn a thread for each request made to the server. If that thread needs to get data from a database that takes a while or another request comes in, that thread is locked up and consuming resources. For a data intensive app with many concurrent connections, that could severly impede the server’s performance.

Node.js is single threaded (yes only one thread!) and is beneficial in applications where frequent blocking I/O requests are made that rely on another part of the server system to complete the action before it can continue.  

Node.js, has only the single thread that processes each request as it comes in. That is by design. When the thread needs to do some kind of action that could potentially lock up the system, Node.js initiates that request with a callback for when the data is returned and releases the resources it is using to maintain that request. When the data is returned, the callback is initiated and the process continues. In the meantime Node.js (and the server) has been able to service other requests and perform other actions. This in turn prevents unnecessary CPU cycles waiting for the data, let alone preventing any kind of performance issue due to a lack of resources.

In an example of a web application receiving data from a web form, there may be many actions that need to be done based on that data. One such action may be to check if the data is valid by comparing it against logic defined by the programmer. For simple applications, this may be done all in the script, in which case Node.js is no more beneficial than any other server side language. However, the real benefit of Node.js comes when that server side script needs to interface with another system such as a database, a disk, another network or a combination of these, where the script would otherwise sit and wait (consuming server resources) for that particular system to do its thing before returning the data.  

So continuing the example of the data from a web form, the data now contains a username and password that needs to be validated against user data in a database. Node.js, as mentioned before, uses an event driven model via the callback. When the request is made to the database to compare the username and password, Node.js releases the action, sets the callback and continues on to the next line of code in your app. When the database completes execution of that particular query and returns the data, the script jumps to the callback and executes the code contained within it.

The example below (using semi-pseudo code) demonstrates how Node.js works. It's more than likely that assuming a valid username AND password, the output would be:
Your username is OK!
                      Correct Password!

Open in new window

This is due to Node.js not waiting around until the database has finished the query and has moved onto the next line of code (checking the username).
// validate the username and password
                      // query the database
                      $db->query("check passwords;", function() {
                         if ($password is not Valid) {
                             alert('Incorrect password');
                         else {
                             alert('Correct Password!');
                      // check the username is valid
                      if ($username is not Valid) {
                         // error here
                         alert('There is an error with your username');
                      else {
                         alert('Your username is OK!');

Open in new window


What are the Roadblocks?

One of the biggest issues is that many hosts do not support Node.js yet, meaning you need to manage your own host instance through the likes of Digital Ocean, Amazon Web Services and other PaaS (Platform as a Service) entities. I’ve always felt uncomfortable having to take care of the security of my own server, which is why I’ve always gone with a host that does it for me. Worth paying for in my opinion.

The learning curve makes it a difficult language to pick up and run with. 

The whole flow of your application will be affected by the way Node.js works. It isn’t always obvious how your code is going to execute and in what order, which may lead to situations you may want to avoid such as nesting all your logic into one event callback after another.

A Modular Approach

I wanted Node.js as my webserver, but I was so used to frameworks and plugins being readily available with languages like PHP that I found it quite an effort getting a full blown web server with basic routing and other general web server related processes to be invisible to the web site I was building. I didn’t want to have to know how the server worked just to build a basic site. I’m a firm believer in a modular approach so I adapted Sails.js to do that for me. Sails.js is based on the MVC methodology making is very easy to set up a site without having to code your own server.


The important aspect with Node.js is determining whether it is suitable for your application and will benefit you with any performance aspect that would outweigh the cost of implementation. With limited web host support, it will often mean a higher cost in hosting and maintaining the server yourself (or paying someone else to), but if your site needs the intense data I/O capabilities as I’ve outlined above and you want the whole web site package, then Node.js is for you.
RobOwner (Aidellio)
I'm an enthusiastic IT Business Solutions Provider that designs and creates a solution YOU want for you and your customers

Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.