Estimate project

What Is Node JS and How Does It Work?

Software Development   -  

December 13, 2019

Table of Contents

So, you are wondering “what is Node JS?” and how does it work. Let’s take a look at our newest article to find out.

What Is Node JS?

Node.js or Node is an open-source and cross-platform runtime environment for executing JavaScript code outside a browser. Quite often we use Node to build back-end development (also called APIs – Application Programming Interfaces).

What is Node JS used for?
What is Node JS used for?

These are the services that power client applications such as a web app (running inside a browser) or a mobile app (running on a mobile device). These client apps are simply what the user sees and interacts with. They are just the surface. They need to talk to some services in the simple server or in the cloud to store data, send emails, and push notifications.

Node is ideal for building highly scalable, data-intensive and real-time backend services. But using why Node even if there are so many tools and frameworks out there? What is so special about node?

Here are some of the reasons:

  • Great for prototyping and agile development
  • Superfast and highly scalable
  • Javascript everywhere. Frontend developers can reuse their javascript skills and become a full-stack developers without the need for learning new programming languages
  • Cleaner and more consistent codebase because javascript can be used both on frontend and backend (same naming convention, same tools, same best practices.. )
  • A large ecosystem of open source libraries is available. It is very likely that there is some free and open-source Node js library out there that could be used in many features of the app that you’re building

Node Architecture

Before Node, we can only use JavaScript to build applications that run inside a browser. Browsers that have a JavasScript engine will take on Javascript code and convert it to something that a computer could understand.

For example, Microsoft Edge uses Chakra, Firefox uses SpiderMonkey, and Chrome uses V8.

browsers are builded by Node JS

And because of this variety of engines sometimes javascript code can behave differently in one browser to another. The browser provides a runtime environment for the Javascript code.

The browser provides a runtime environment for the Javascript code.

Before the year 2009, the only way to execute Javascript code was inside a browser. In 2009, Ryan Dahl – the creator of Node came up with a brilliant idea. He thought it would be great to execute JavaScript outside of a browser. 

So he took the Google V8 engine (which is the fastest JavaScript engine at that time) and embedded it inside a C++ program and called that program Node. So similar to a browser, Node is a RUNTIME ENVIRONMENT FOR JAVASCRIPT CODE. It contains the V8 Engine that can execute Javascript code but it also has certain objects that provide an environment for our Javascript code.

But these objects are different from the environment objects in the browser. For example, we don’t have the ‘document’ object instead, we have other objects that give us more interesting capabilities like the ‘fs’ object for working with the filesystem or the ‘HTTP’ object for listening for requests in a given port.

Node is a program that includes the V8 Javascript engine plus some additional modules

In essence, Node is a program that includes the V8 Javascript engine plus some additional modules that give us capabilities not available inside the browser.

Both Chrome and Node share the same V8 engine but they provide a different runtime environment for Javascript.

Some people are comparing Node to C# or Ruby. But these comparisons are fundamentally wrong because NODE IS NOT A PROGRAMMING LANGUAGE. Node also should not be compared with frameworks such as ASP.NET, Rails, etc. Node is NOT a framework. It’s simply a runtime environment for executing Javascript code.

How Does Node JS Works?

Node apps are highly scalable because of the non-blocking (asynchronous) nature of Node. But what does that mean?

1. The Non-Blocking (Asynchronous) Nature of Node

Imagine you’re going to a restaurant. A waiter comes to your table, takes your order and gives it to the kitchen, then he moves on to serve another table while the chef is preparing your meal. So the same person can serve many different tables. He doesn’t have to wait for the chef to cook one meal before serving other customers. This is what we call non-blocking or asynchronous architecture. This is how a Node app works.

The Non-Blocking (Asynchronous) Nature of Node

The waiter is like a Thread allocated to handle a request. A single thread is used for handling multiple requests. 

In contrast to non-blocking (or asynchronous) architecture, we have blocked (or synchronous) architecture.

2. Blocked (Or Synchronous) Architecture of Node

Let’s see how that works. Back to our restaurant example, imagine you go to another restaurant and a waiter is allocated to you. After receiving your order, he gives it to the kitchen and now he’s sitting in the kitchen waiting for the chef to prepare your meal without doing anything else. He will not take any other order from any table until your meal is ready.

This is what we call blocking or synchronous architecture. And that’s how an application built with frameworks like ASP.NET or Rails works out of the box.

Let’s take a look at a technical example: When we receive a request on the server, a thread is allocated to handle that request. As part of handling that request, it’s likely that we’re going to query the database, sometimes it might take a while till the result is ready. When the database is executing the query, that thread is waiting there, and it can’t be used to serve another client. We have to use another thread for that.

Imagine what would happen if we have a large number of concurrent clients? At some point, we will run out of threads to serve them. All of our threads are busy. As a result, new clients have to wait until free threads are available. Or if we don’t want them to wait, we need to add more hardware. We’re not utilizing our resources efficiently. This is the problem with blocking (or synchronous) architecture. That’s how applications built with frameworks like ASP.NET, Rails work by default. We could make them asynchronous but we have to do extra work.

3. Node Applications Are Asynchronous by Default

So we don’t have to do extra things. In Node, we have a single thread for handling all requests. If we need to query a database, the thread doesn’t have to wait for the database to return the data. While waiting, that thread could be used to serve another client. 

Node Applications Are Asynchronous by Default

When the database prepares the result, it puts a message in an Event Queue. Node is continuously monitoring this queue in the background. When it finds an event in this queue, it will take it out and start processing. This kind of architecture makes Node ideal for building applications that include lots of disks or network access. We could serve more clients without the need of throwing away more hardware. And that’s why Node apps are highly scalable.

Conclusion

In contrast, Node should not be used for CPU-intensive applications like Video encoding or Image manipulation services. Those apps have a lot of calculations done by CPU and few operations that touch the file system or the network. Since Node apps are single-threaded, when performing the calculation to serve one client, others have to wait. That’s why Node should not be used for CPU-intensive apps and only be used for building Data-intensive and real-time applications.

We at Designveloper hope this article has answered your question “what is Node.JS?”

Also published on

Share post on

Insights worth keeping.
Get them weekly.

body

Subscribe

Enter your email to receive updates!

Got an idea? We can help you realize it.
Submit
body

Subscribe

Enter your email to receive updates!