Node.js – reasons to use, pros and cons, best practices!

JavaScript has certain characteristics which makes it unique from the other dynamic languages. It has no concept of thread, but its model of concurrency is completely basing on events; i.e. it’s an event driven programming language.
The same thing makes node server different than other web servers. This article describes what Node.js is, and how it is different and can be one of the most popular and powerful servers in future.

node processing model

What is Node.js

In layman’s word, node is just another server like Apache, IIS, TOM etc. But unlike those servers Node doesn’t deal with PHP, .NET or JAVA. It executes JavaScript in the server side. Yes, JavaScript without a browser; that’s Node. Node itself is not made in JavaScript completely; rather its wrappers are made in C. It just executes JavaScript. Below is a list, what Node is not and what Node is.

What node is NOT

  1. Node is not a framework, it’s a server.
  2. Node wrappers over JavaScript V8 Runtime, are not made in JavaScript, but made in C.
  3. It’s not multi-threaded. It runs in a single thread with callback concept.
  4. It’s not for JavaScript beginners as it’s very low level.

What Node is

  1. Node is a server which can execute JavaScript. Sort of a server side browser.
  2. Node is a open source, cross platform to make real time network applications.
  3. It provides you asynchronous, event driven I/O APIs.
  4. It runs single threaded event based loop, so all executions become non-blocking.

Pros and Cons of Node.js

Pros Cons
1. Asynchronous event driven IO helps concurrent request handling. 1. Node.js doesn’t provide scalability. One CPU is not going to be enough; the platform provides no ability to scale out to take advantage of the multiple cores commonly present in today’s server-class hardware.
 2. Uses JavaScript, which is easy to learn. 2. Dealing with relational database is a pain if you are using Node.
 3. Share the same piece of code with both server and client side. 3. Every time using a callback end up with tons of nested callbacks.
 4. npm, the Node packaged modules has already become huge, and still growing. 4. Without diving in depth of JavaScript if someone starts Node, he may face conceptual problem.
 5. Active and vibrant community, with lots of code shared via github, etc. 5. Node.js is not suited for CPU-intensive tasks. It is suited for I/O stuff only (like web servers).
 6. You can stream big files.

Why to use Node

Non-blocking code

This is the strongest reason to select node as your server. Node is completely event driven and majority of the code runs basing on callbacks. This approach helps the application not to pause or sleep, but to become available for other requests. Let’s explain this with an example.

Suppose the program will read a .txt file and print the content as output.

In php

<?php
	echo "started \n";
	$content = file_get_content ("myFile.txt");
	echo "myText says: " + $content;
	echo "finished";
?>

In Node

var fs = require('fs');
console.log("started \n");
fs.readFile("myFile.txt",function(error,data){
	console.log("myFile.txt says:" + data);
});
console.log("finished");

If you notice the code, you will find in php, in line no#3, it will start reading the file and before finishing up reading the file it can’t do anything else. That means php blocks the execution there. Whereas in case of Node, in line no#3 it starts reading the file and assigns a callback to it, and proceeds to the next line. It continues executing the other parts of the program and whenever the “fs” finished reading the myFile.txt, the callback will be called to process necessary statements.

Fast processing

Node uses V8 JavaScript Runtime engine, the one which Google Chrome uses. Node has a wrapper over this JavaScript engine providing some extra facilities to build network applications. Both the Node wrapper and the V8 JavaScript engine are written using C language; which makes it really fast. It is much faster than Ruby, Python, or Perl.

Concurrent request handling

Node can handle thousands of concurrent connections with a very minimal overhead on a single process.

One environment

Using JavaScript on a web server as well as the browser reduces the impedance mismatch between the two programming environments which can communicate data structures via JSON that work the same on both sides of the equation. Duplicate form validation code can be shared between server and client, etc.

Easy to learn

Node is not something new. It’s just simple JavaScript. So the JavaScript developers don’t have to put much extra effort to learn Node.

Popularity and community

A lot of people already know JavaScript, even people who do not claim to be programmers. It is arguably the most popular programming language. So a popular language means a popular community. Big community means easy to get help or support.

Where Node can be the best solution

Here are few applications or areas where Node.js can be the best solution.

Web Socket Server

The non-blocking architecture of node makes it the best suited solution for socket server applications or broadcasting like applications. Chat servers can become more efficient and real time using Node.js as their base.

Fast file upload client

With node you can upload multiple files at a time. That means it is possible that a part of file1 and another part of file2 is in the server at a given point of time. This approach makes file uploading dramatically fast.

Data streaming

As node deals with callback concept, it can easily be used for streaming data flow. It can be really useful for Travel industry where they fetch results from different APIs of different suppliers.

Ad server

Well, I personally think Ad servers should be the fastest servers. Because, from a advertiser’s point of view, if I can load the ads before all the contents of the page is loaded, I may catch the visitor. Another is if my ads load slower, the visitor might navigate to somewhere else. So ad servers really need to be fast and thus Node should be used.

Stock exchange software

Things has to be very much real time in case of stock updates. Node helps us to develop real time web applications.

Conclusion

Node.js came up with a new idea of event driven single threaded server programming which is achieved with callback concept. With the growing demand and popularity of JavaScript, a step to reach the server side or backend programming is really appreciable and it also raise hope to lots of JS developers. But at this moment, if we say Node is strong enough to replace .NET, PHP or JAVA it will be a hyperbole. But yes, the way JavaScript is developing, we are very hopeful that one day Node may become an ultimate solution for the web backend.


Still having issues?

If you still have issues understanding the above article, please leave a comment below and we will try to address that. In case you need help in your projects or understanding anything related to Programming; contact me, Paul Shan for assistance. Thank you for being a reader of VoidCanvas.

About This Author

Hello! I am Paul Shan, a JavaScript Expert, Full Stack and DevOps Engineer cum Consultant based out of Bengaluru, India.

  • n king

    You comparison with PHP is flawed.
    PHP is a language Node is an application, you are comparing apples & pears
    There are quite a few non-blocking PHP applications/frameworks out there, & have been for many years, most based around POSIX threads.
    So the non blocking example you gave could easily be written in PHP.
    You could even flip the example using a Synchronous JavaScript file library & an evented PHP threaded applications/framework.
    Giving the completely opposite conclusion.

    • metalshan

      PHP is a language and Node is an application … I agree with that. But people started considering Server Side JavaScript as node, that’s the reason I am using the word node instead of “Server Side JavaScript”.

      You know node has a plugin; I think the name is cluster; which claims to provides you multi threaded javascript. But do you know what it internally does? It used to create different js processes. So, though it looks like it is making the things we want, but actually it is creating unnecessary processes to make the application heavy.

      I know about PHP but I’m not an expert in that. So commenting on those PHP frameworks you mentioned will be unfair. But I can tell you that they are just providing you the features you want, not the way you want. They are providing you hacks, just like cluster provided to javascript users.

      Now about synchronous JavaScript. Do you know if you try a complete synchronous library and it is waiting for any response or something, it will completely hang the entire application cause javascript has a single thread.

      See, workarounds are always there. That’s the reason people are publishing the libraries. Creating features are not important, but making it in the right and efficient way is. That’s why we should compare languages on the basis of their nature, their way of functionality and not by the fixes or workarounds provided by others.

    • n king

      Not sure what your trying to tell me?
      Yes thats what ‘synchronous’ means!
      Certainly in some browsers they go a step further and halt other browser functionality, like css transitions … even if they were initiated before the synchronous JavaScript.

      No ‘hacks’ in the PHP applications I was taking about, ‘proper’ multi-threading, management of those threads via PHP code.
      Not some hack like thread simulation via generators.

      Only used in high traffic, socketed PHP applications ‘always on’, with threadpool.
      PHP is a language, with some howlers, not surprising given its age & the evolution of the web, but you can say that about every language in time.(and some sooner Golang)

      Its not about the ‘best language’ unless your a noob, but about the ‘best tool for the job’;
      This spans many aspects not just language features.
      A lot are in the business realm; cost, speed of implementation, speed of deployment, qualified staff, existing implementations….

      I certainly wouldn’t write banking software in PHP (not even Hack) or JavaScript

    • metalshan

      Did I wrote an article on why you should choose JS over PHP? I wrote one showing pros and cons. Having a non-blocking code was a basic advantage.
      “Horses for courses” – every programmer knows that, there is no meaning of shouting lectures on that. Neither I say for every course use JS, not people are running on PHP horse. See your first question what you asked and read the answers in my ans on top.

    • n king

      After some research, i realise you have plagiarized / copied parts of the Wikipedia page on Node.js, which gives the same faux argument.
      I will submit an edit to Wikipedia to correct