The What and Why
Let’s start at the very beginning: What exactly is Node.js?
Node is better defined as a wrapper around a VM like V8.
That’s the simple story, but Node.js is more useful than just that. Node comes with some handy, built-in modules providing easy-to-use asynchronous APIs. Let’s talk about that, and a few other reasons why developers are picking Node.js over many other options when it comes to creating services for their backends.
1. Native Modules
Node comes with feature-rich modules that make it a great platform for hosting and managing servers. These modules offer features like reading and writing files, consuming data over the network, and even compressing and encrypting data You don’t need to install these modules. They come natively packaged with Node!
The big deal about these modules is that they offer asynchronous APIs that you can just use directly without worrying about threads. Yes! You can do asynchronous programming in Node and do things in parallel without needing to deal with threads. This is a big deal and it’s probably the most common benefit of using a runtime like Node.
The asynchronous nature of these modules works great with VMs like V8 because these VMs are all single-threaded. This is true for both Node and Browsers. You only get a single precious thread to work with. It’s extremely important to not block that thread. For example, in your browser, if your website blocks that single thread for, say 2 seconds, the user cannot scroll up and down during these 2 seconds! In Node, if an incoming HTTP connection to your web server was handled synchronously rather than asynchronously, that’ll block the single thread, and your whole web server cannot handle any other connections while the synchronous operation is active. That’s very bad.
Even if you don’t want to host your backend servers in Node, having the powerful, built-in features — and the ease to add more — makes Node a great platform for tools to work with other applications and enhance your work flow.
2. A Package Manager and a Dependency Manager
NPM is basically the world’s largest collection of FREE and reusable code. You can make a feature-rich Node application just by using code that’s freely available on NPM. NPM is a reliable package manager which comes with a simple CLI (the
npm command). The
npm command makes it really easy to install third-party packages, share your own code, and reuse your own code. In addition, the NPM registry, where the packages get hosted, has so many options. By “so many”, I mean hundreds of thousands of options of FREE tools that you can just install and use on your Node servers.
3. One Language to Rule Them All
One language means less syntax to keep in your head, less APIs and tools to work with, and less mistakes over all.
One language means better integrations between your frontend code and your backend code. You can actually share code between these 2 sides. For example, you can reuse your frontend UI components code to render server-side HTML.
There will be dragons!
Node is not all rainbows and unicorns. It has some disadvantages, which are interestingly the same advantage points if you just look at them with different bias. For example, Node’s asynchronous non-blocking nature is just a completely different model of thinking and reasoning about code. If you’ve never done it before, it is going to feel weird at first. You need time to get your head wrapped around this model and to get used to it.
Having a big package registry offering many options means that for every single thing you need to do you have many options to pick from, and some people hate that. You need to constantly research these options and make a mental effort to pick the “better” ones. These options usually have big differences and you might end up spending a lot of time researching them.
Also, NPM along with Node’s module manager enabled shipping smaller and smaller code. This means you need to use more and more packages. It’s not unusual for a Node application to use 300 or more packages. This is both a good thing and a bad thing depending on who you ask. I think it’s a good thing. Smaller packages are easier to control, maintain, and scale, but you do have to make peace with the fact that you’ll be using a lot of them.
|Smaller code is actually why Node is named Node! In Node, we build simple small single-process building blocks (nodes) that can be organized with good networking protocols, to have them communicate with each other and scale up to build large, distributed programs.|