What is Node.js?
Characteristics of Node.js:
Asynchronous and Fast Execution
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.
When should you use Node.js?
There are a number of times you should use Node.js. These include:
- IO bound apps
- Data/Video streaming
- Single Page Applications
- JSON APIs based applications
- 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.
- Create a directory called nodejs (you can create it on your desktop).
- Create a hello.js file inside nodejs containing the following text:
- Required: It is a directive that is used to load various modules. Here we are using it to load http module.
- 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:
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.
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.