nodejs

What is Node.js used for?

nodejs
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?

Node.js is a fast, lightweight, server environment which is free, runs on different platforms and uses JavaScript. To know how Node.js came about, I would like to guide you through a little history lesson.

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

JavaScript was born which made it possible to interact with web pages by creating cool hover effects, animation and manipulating data. Combined with a server-side language, you could create applications that run in the browser. No need for downloading, installing, etc. Just type in the address and you’re good to go.

Not long after, you could read your emails on the web, handling your finances and do your shopping online.

AJAX

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.

AJAX, which stands for Asynchronous JavaScript and XML, solved this problem. With AJAX you could actually just get the data from the server, and dynamically insert data into the page with HTML tags and formatting, taking a lesser toll on the bandwidth.

Google Chrome

The people at Google still thought the web was to slow. In 2008, they released a new web browser called Chrome.

What was most revolutionary about this browser is that they created a new JavaScript engine from scratch, which they called “V8”. This engine was highly optimized compared to other engines. The V8 engine is fast because it compiles JavaScript directly to native machine code before executing it. In addition, the compiled code is optimized during runtime.

V8 JavaScript Engine and Node.js

In 2009, the creator of Node.js, Ryan Dahl had an idea of using JavaScript on the server by using the V8 JavaScript engine. So that’s what he did. He created a layer around the JavaScript engine to work on the server and called it Node.js.

What are the benefits of using Node.js?

JavaScript can be used on the client and the server

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

Node.js has it’s own package management system. This is gigantic with hundreds of thousands of JavaScript libraries/modules. Those libraries can be used both on the frontend and backend. This is the industry standard of publishing and acquiring libraries.

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?

It uses JavaScript on the server

I mentioned previously that running JavaScript both on the frontend and the backend is a major advantage. But JavaScript in isolation is error prone.

Backend languages like C# use types and the code gets compiled before it is able to run. This way you will get notified of errors during development. Since JavaScript doesn’t have types and doesn’t compile the same way, you will often get an unpleasant surprise during run-time.

There are ways to combat these issues though. Something like TypeScript can be used instead of  JavaScript. TypeScript is basically JavaScript with extra features, like providing type checking to your code. There are also linting tools which can notify errors in your JavaScript code.

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:

  • Wallmart
  • Uber
  • eBay
  • NASA
  • Yahoo

Conclusion

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.

On the other hand, if your backend does heavy calculations like image processing or simulations, you should look elsewhere. Node.js are asynchronous on I/O operations, but not on your JavaScript code.

 

 

Similar Posts