You may have heard about Node.js, but what is it? And what is Node.js used for? When should I use it?
Node.js is mostly used as a backend web server which exposes API’s your front end can communicate with.
A Node.js application can do things like:
Open/write files on the server.
Adding, deleting and getting data from a database.
Handling real-time messaging from multiple users for apps such as HTML5 multiplayer games or chat rooms.
And much more…
What is Node.js?
In the beginning…
In the early days of the internet, a popular protocol called HTTP was born. Soon after there were web browsers like Netscape which used this protocol to display web pages. These pages were very simple with mostly bad design, but this was a revolution since people could create pages with information. These pages were then accessible to people all over the world.
But as we know, technology keeps pushing forward and soon, static boring web pages just wasn’t enough.
Dynamic web pages
Not long after, you could read your emails on the web, handling your finances and do your shopping online.
Previously, when getting data from a server, you needed to load the entire page. This means images, HTML tags, everything. Often you would only like to load a section of your page.
The people at Google still thought the web was to slow. In 2008, they released a new web browser called Chrome.
What are the benefits of using Node.js?
This is a major benefit for companies since they can use the technology people already know. Now the front end developers are able to transition into backend development very easily and vice versa.
Often when you are working on the frontend, you might need something from the backend to finish your task. If your company only have dedicated backend developers in a language you don’t understand, you might need to ask them to implement it and hope it won’t take too long.
Having the opportunity to develop from both the backend and the frontend will, therefore, speed up the development big time. No wonder a lot of companies are embracing Node.js.
Node.js can handle a large number of real-time users
Traditional backend servers often spin up new processes or threads for each user connection. If you have thousands of users connecting at the same time, your memory usage will go through the roof.
Node.js runs on a single thread. This means when the server executes code, this has to finish before another execution can be made. However, this only applies to code which does things like calculations, etc. I/O operations like reading a file or getting data from a database run asynchronously. This means when Node.js does a database call, other code can execute in the meantime. When the data from the database is fetched, it continues running on a callback function.
It is the event loop which handles these I/O events and returning those callbacks. This event loop is why Node.js is called “event-driven” and the asynchronous I/O calls is why Node.js is non-blocking.
Node.js package management
The great thing is that you can easily install new packages from the command tool and update to a new version is a breeze. When installing packages to your project it goes into the folder called “node_modules” and information about your packages get stored in a package.json file. When pushing the project to a repository like Github, you don’t need to include the library files. Only the package.json file is necessary. When users clone your Github project, all they need to do is run “npm install”, and all the packages from the package.json file gets installed.
If you need to install for example the lodash library to your project. All you need to do is type “npm i –save lodash” from your command tool, and you’re good to go.
Fast with low resource usage
As I have mentioned, Node.js is event-driven and non-blocking. Since it doesn’t have to create multiple processes or threads makes it fast with very low resource usage, as long as the logic isn’t CPU heavy.
What are the drawbacks of using Node.js?
Nested callback hell
Node.js relies heavily on callbacks when doing I/O operations. There are a lot more I/O operations on the backend compared to the frontend. A common issue which can arrive is nested callback hell. This is when you do an asynchronous call, which returns a callback, which in turn do another I/O operation, which returns another callback and so on.
This may result in messy code with callbacks nested in what in the end looks like a pyramid. If this happens your code will be hard to read, and error handling would get quite messy.
Luckily, with some clever coding, there is no problem avoiding this issue. Read more about callback hell here.
Tons of packages
The Node.js package management system is excellent. But with a ton of packages, it is easy to get lost.
If you don’t know what you’re looking for it might be hard to get what you want. There are a lot of packages that do similar things, and there is no rating system which helps you find quality packages.
Some of the packages might not be as mature as you would like. This may result in unwanted bugs and missing functions.
But on the other hand, this is what is great about this open system. Giant library open for everyone!
Doesn’t work so well on CPU intensive tasks
Since Node.js is single threaded, it doesn’t work that great in CPU intensive tasks. Although the I/O operations are asynchronous, plain code executions are not. So if your code is doing some heavy calculations, then Node.js might not be the optimal choice since it then will block your thread.
Who uses Node.js?
Netflix might be the most famous company which are using Node.js. They have millions of subscribers using the system, which proves that Node.js scales very well.
Paypal migrated their backend from Java to Node.js. They had divided their team with those who worked on the client and those who worked on the server. With the migration to Node.js, they could have one team with the same set of skills working together.
LinkedIn moved their mobile backend from Ruby on Rails to Node.js. Improving the speed of the application dramatically.
Some other notable companies using Node.js:
If you need a backend which can handle many users with simple logic, Node.js is a great choice. Your resource usage on servers will be very low, which reduces server costs.