October 23, 2015
  |   Blog, Technical Development

Getting to know Node.js

What is Node.js?
It’s an open source cross-platform runtime environment for server-side web applications built on Google Chrome’s javaScript Engine (V8 Engine). Its official documentation can be found here.  

Node.js uses non-blocking, event-driven I/O. Its lightning fast speed (due to fast Javascript execution) coupled with tremendous scalability makes it really efficient for data-intensive real-time applications and scalable network applications capable of handling a high number of simultaneous connections with high throughput.

Characteristics of Node.js:

Asynchronous and Fast Execution

Want to make performance-efficient apps? Here is how Node.js can help. All APIs of Node.js are non-blocking (asynchronous). Thus, instead of waiting on one API for a response, the server moves onto calling the next API and uses event notification to get the response from the first one. We will explain this with an example later. Node.js is built on top of V8 Javascript engine, which makes it fast. The V8 engine compiles javascript directly into native assembly instead of interpreting it into an executable bytecode. By removing the overhead of interpretation, the engine enhances performance.

Single Threaded

Unlike traditional servers that spin a limited number of threads to handle requests, Node.js uses a single threaded program to service a much larger number of requests using a mechanism called event-looping.

Highly Scalable

The javascript that Node runs is single threaded, but a lot of the tasks you execute in Node—such as network or file io—run in background threads. This makes it open to receiving more requests. Node.js has proven to scale up to a million concurrent connections.

When should you use Node.js?

There are a number of times you should use Node.js. These include:

  1. IO bound apps
  2. Data/Video streaming
  3. Single Page Applications
  4. JSON APIs based applications
  5. Real Time, multi user applications, such as chat

When not to use Node.js?

CPU intensive apps: Since Node.js uses a single thread to service all requests, there is a high likelihood that long running CPU tasks will block the whole server. If you still need to have “long-running” tasks, it’s best to push them to some asynchronous web worker.

Who is using Node.js?

Some of the tech giants that are using Node.js are eBay, General Electric, Microsoft, PayPal, Uber, Wikipins, Yahoo!, Heroku, LinkedIn. For a complete list, click here.

Setting up your environment:

    • You can download the latest installable archive file here. At the time of this blog, the latest Node.js version is v4.1.0.
  • Mac users can also install node.js using homebrew. NPM is bundled along with node when you install it using homebrew.

A quick Hello World!!

This is a code sample for a quick hello world application. This code creates an HTTP server which listens ie. wait for a request over 8081 port on local machine.

  1. Create a directory called nodejs (you can create it on your desktop).
  2. Create a hello.js file inside nodejs containing the following text:
  3. Required: It is a directive that is used to load various modules. Here we are using it to load http module.
  4. Navigate to your “nodejs” directory. Run this command in terminal: node hello.js

Hurray! Your first node.js application is ready.

What makes Node.js highly Scalable?

Node.js intensively uses Callbacks.

What are Callbacks?

Node.js is an async platform. What that means is, it doesn’t wait around for operations like I/O to complete their execution. It uses callback, which is a function called at the completion of some task. This allows other requests to be served in the meantime, thus preventing any blocking.

Callbacks are particularly important since Node is single threaded. If there is a process that could take a long time to execute, it can be dealt with in a non-blocking way.
For example, consider a function that is needed to perform an I/O operation to read a huge file. If Node.js waits on it, it cannot serve any other request in that duration. But, if the control is returned to the execution environment immediately, there is no blocking for I/O and Node.js can process other requests in the meantime. Once the I/O is completed, it will call the callback  function with the contents of the file as function parameters. The following section will demonstrate this.

Blocking vs. Non Blocking:

The following example will explain the sync and async calls that we mentioned in the characteristics section. The blocking.js file ensures that the call is synchronous and the program will proceed only after it has finished reading the file. Whereas nonblocking.js allows asynchronous execution thus the program is not blocked on the file and prints “Thank you!” first.
First create a text file with contents similar to the image below:

Then, create a blocking.js file in the same directory to read this file synchronously:

Next create a nonblocking.js file in the same directory to perform asynchronous I/O:

Now, execute the two files using the command “node blocking.js” and “node nonblocking.js”
Compare the two outputs:

nonblocking.js

blocking.js

We see that in blocking.js, the program halts until the file I/O is complete where as in nonblocking.js, it continues executing and prints the “Thank you” message while performing I/O in the background. The “fs” that you see in the code is the filesystem module which is built in Node.js to perform I/O tasks.

Conclusion:

Node.js is an extremely powerful and efficient technology that can suit a variety of use cases (remember, no CPU intensive jobs). It is an excellent choice for high load applications. Big companies like Netflix, LinkedIn, PayPal are basing their servers on Node.js. Its community is a huge influencing factor. It has some 192K modules in npm, which shows how active the community is. Leveraging the numerous modules available in npm anyone can start building complex apps in no time.
This was an introductory post about Node.js. If you have any problems or further questions, feel free to contact us. Stay tuned for upcoming posts on creating web applications using MEAN stack.

Subscribe to Our Newsletter

Get the latest insights from Blue Acorn iCi

Let's build something together.