Understand Node.js


JavaScript was popular client-side programming language that runs in the browser, right? Not any more. Node.js is a way of running JavaScript on the server.

Yes, you can develop end to end web applications using JavaScript now.

Node.js is a JavaScript runtime system. It is implementation of JavaScript engine (named V8) that runs inside of Google Chrome. It allows you to run JavaScript from the command line instead of in your browser.

Essentially it is made up of

  1. Google Chrome V8 JavaScript engine – super-fast JavaScript interpreter
  2. A thread pool to handle file I/O operations. All blocking system calls are processed here.
  3. An event loop library to provide non blocking capability
  4. Node bindings to bind C/C++ library (for above 3) to JavaScript library (node modules)
  5. Standard Node Library.


Why Node.js

Top 3 reasons why I think, Node.js is better option than conventional client-server model for an enterprise application.

  1. Scaling : In traditional approach, we have an underlying Apache or NGINX or Web Sphere web server or Application Server, with Java or PHP running on top of it. Each new connection to the server spawns a new thread, and it’s very easy to quickly lose performance – the only way to support more users being to add more servers. It simply doesn’t scale well. With Node, this isn’t the case. There is no underlying web or application server to listen for incoming connections and return HTTP status codes – you’ll need to handle that core server architecture yourself in the application code. Hard? Luckily, there are modules to make this easier. You just pick and use a module.

  2. Event Based Approach: In traditional approach, the server side program has to wait whilst each function completes before going on to the next. This means, if one user triggers a heavy functionality can impact the experience of other user.  JavaScript is an event-based language, so anything that happens on the server triggers a non-blocking event. A core concept of Node is asyncronous functions – so everything runs in the background, basically. You specify functions that should be run on completion of something else, while the rest of your app moves on.

  3. End to End JavaScript: Be it client side programming or server side, it is all same language. So, learning curve is small. We have an incredibly fast platform (thanks to node.js) with a programming language that’s incredibly easy to use (thanks to JavaScript).

Why Not Node.js

Node.js really shines in building fast, scalable network applications, as it’s capable of handling a huge number of simultaneous connections with high throughput, which equates to high scalability. See this example

But, We can’t rely on Node.js always. I believe, following are some cases where Node.js may not be an ideal solution

  1. Heavy / CPU intensive work – Node.js, by default, is a single thread model uses single core CPU. Node.js offers some level of clustering to make use of multi core CPU architecture but it is still in initial stage. Currently, people run several Node.js server instances to achieve multi tasking. With clustering, it is possible to offload all heavy computation to background processes (batch jobs) written in a more appropriate environment for that, and having them communicate via a message queue server like RabbitMQ.
  2. Huge File I/O work – for the same reason above.

Node Modules

Node uses common.js for modules – Modules are an integral piece of any robust application’s architecture and typically help in keeping the units of code for a project both cleanly separated and organized.

In JavaScript, the Module pattern is allows you to emulate the concept of classes so that we’re able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope.

What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page.

With modules, you can sandbox the imported functionality in a locally name spaced variable. e.g.

var logger  = require(‘some logger module’);

Also, you can explicitly declare your dependencies with modules (this is very imp feature) – importing Module A transitively imports dependent Modules B and C also.

ES6 made modules are part of JS specifications now. You can define classes in JS

NPM (Node Packaged Module)

Node installation, by default, comes with NPM.  NPM modules are publicly available, reusable components written by community for various regular tasks. These modules are available through easy installation via an online repository, with version and dependency management.

All these module can be found @ https://npmjs.org/ and these can be installed using command line interface that comes with node installation.

Few examples of  NPM modules are:

  • express – Express.js is web development framework for Node.js used by majority of Node.js applications.
  • connect – Connect is an extensible HTTP server framework for Node.js, providing a collection of high performance “plugins” known as middleware; serves as a base foundation for Express.
  • socket.io and sockjs – Server-side component of the two most common websockets components out there today.
  • mongo and mongojs – MongoDB wrappers to provide the API for MongoDB object databases in Node.js.
  • underscore (lodash, lazy) – The most popular utility library in JavaScript, packaged to be used with Node.js.

Install Node.js

This is pretty simple. Just get an installer from here https://nodejs.org/en/download/ and follow the on screen installation instructions. Once installed, you can check the versions to make sure all is well.

Hope you enjoyed this post. You may want to try our MEAN stack experiment here.

Node Version Check


Do you mind leaving a reply?

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s