This week I’ve been learning a little bit more about Node.js. I’ll cover what I learned and why this is used.
Quite often we use Node to build backend services also called API’s(Application Programming Interface). These are the services that power our client applications like a web app running inside of a web browser or a mobile app running on a mobile device. These client apps are simply what the user sees and interact with. But these services need to talk to some services sitting on the server or in the cloud to store data, send emails or push notifications…
Why do we use Node?
Node is ideal for building highly-scalable, data intensive and real time backend services that power our client applications. So why do we use over other backend services such as Rails, Django… ?
- Node is easy to get started and can be used for prototyping and agile development.
- It can also be used for building super fast and highly scalable services. It’s used in production by large companies such as Paypal, Netflix, Uber, Walmart, and so on.
In fact, some studies found out that:
- we can build twice as fast with fewer people,
- in 33% fewer lines of codes,
- using 40% fewer files.
- They double the number of requests served per second
- while decreasing the average response time by 35%
5. Another reason of using Node is that it has the largest ecosystem of open source libraries available. So for pretty much any features or building blocks you want to add to your application, there is some free open source library out there that you can use.
Architecture of Node
How Node works?
We saw earlier that Node is highly-scalable, and this is because of the asynchronous nature of Node. What this means is that a single thread is used to handle multiple requests.
When the database prepares the result, it puts a message in what is called: “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 process it.
This kind of architecture makes Node ideal for building applications that include a lot of disk or network access. We can serve more clients without the need to throw in more hardware.
In contrast, Node should NOT be used for CPU(Central Processing Unit)-intensive applications like a video encoding or an image manipulation service In these kind of applications, we have a lot of calculations that should be done by CPU and few operations that touch the file system or the network.
How to install Node?
To see if you already have Node on your machine, open your terminal or command prompt and type:
To install the latest version, head over to https://nodejs.org/en/
The recommended version on the left is the latest verified version, the one on the right is the experimental one and might not be stable.
I am in the process of experimenting Node by using a database with MongoDB. I will post about it in another blog.