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

Sat Oct 11 2014

Paul Shan

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

[code language="php"] [/code]

In Node

[code language="javascript"] var fs = require('fs'); console.log("started \n"); fs.readFile("myFile.txt",function(error,data){ console.log("myFile.txt says:" + data); }); console.log("finished"); [/code] 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.

SHARE THIS ARTICLE

post-thumbnail
Today everyone knows the importance of a lightning-fast website and how the speed impacts the conversion rate of a business. Today, everyone wants the site to be a PWA so that the mobile users can have an app-like experience with the website because, for the majority of the merchants, the customers come through mobile devices.
Tue Apr 20 2021
post-thumbnail
Here we are going to see how you can manage backup and restore of Postgres database with docker.
Thu Sep 03 2020
post-thumbnail
Image sliders or carousels always have increased the UI attraction of websites and they are pretty useful for reflecting the major roles/products too. In case, I am having a website that sells tee-shirts,
Mon Apr 30 2018

About VoidCanvas

This blog was created out of hobby and talks mostly about technology, web development, JavaScript, NodeJS and related topics. Thank you for reading my blog.

Copyright 2022 - www.voidcanvas.com

Popular Articles

Authentication using Google's oAuth api with node.js

Thu Mar 10 2016

OAuth authentications are pretty popular now a days and another thing which is popular is JavaScript. This article shows how to plugin google’s oAuth api for authentication in your own node application.

CSS3 Loader Snippet Collection: (Part 2 - Squares)

Sat Mar 01 2014

This is a continuation of my CSS3 loader snippet collection series. I've provided spinning css3 animation loader in the part 1 of this series and here in part 2, I'm providing various square type loading