Node.js is not a JS framework
How it works
If you open the hood of Node.js, you’ll discover the realm of the event-loop. Traditional web-serving techniques stipulate a separate thread for each request. Henceforth, random access memory (RAM) experiences a huge load. As for Node.js web development, the non-blocking input/output model needs a single thread to support multiple concurrent requests in the event-loop without clogging RAM. Simply put, when data exists, it is simply transmitted without constant querying. All asynchronous tasks are taken by the event-loop, which ensures a high level of responsiveness and, hence, speed.
Pros & Cons
In our article dedicated to comparing Python vs. Ruby vs. Node.js, we made a short introduction to the advantages/disadvantages of the run-time environment. Well, if you want to know what is Node.js best used for, a detailed review of its strengths and weaknesses is obligatory.
Node.js has paved the way for JS to the server side. Now, companies and startups can build both the backend and frontend of their products with only one scripting language. In terms of development, you cut your time expenses, as well as recruiting efforts, since a team of JS-savvy engineers might be enough to succeed.
– Asynchronous non-blocking input/output
With Node.js, you have no trouble processing hundreds of thousands of simultaneous requests. The data flow experiences no interruption, which, in practice, gives less RAM consumption and faster performance.
– V8 engine
Though Node.js is not a Chevy Corvette, it has a V8 engine as well. However, it is a JS engine developed by Google. V8 converts JS code into machine code that provides extremely fast execution.
– Microservices architecture
In today’s reality, architecture based on microservices is gaining popularity over the monolithic one. For this reason, a variety of well-known companies including Netflix are taking up the practice of splitting an app into smaller services. Besides, the technology offers ample ready-to-use modules, as well as an event-driven I/O model, to implement microservices solutions.
– Rich ecosystem
The availability of ready-to-use tools for building Node applications is a significant booster for the development performance. For this reason, you should learn three letters – N, P, and M. They refer to the JS package manager, which amounts to over 700K building blocks so far. NPM allows you to search, install, share, and reuse lines of code.
|– Heavy computations incapacity|
Node.js is a great solution for building complex projects. However, it is not the option when you need to deal with CPU-intensive tasks. Due to incoming request blockage by heavy computations, there is a significant loss in performance. On that account, it is not a fit for long-running calculations.
– Callback hell
This issue can affect the quality of your JS code and trigger other declines such as development slowdowns and cost increases. Callback hell is a situation caused by execution of multiple asynchronous operations where myriad nested callbacks end up a callback function. Well, starting with the 7th release, you have the async/await feature to mitigate problems with callbacks. Unfortunately, they do not promise to avoid them completely.
What can you do with Node.js
Early on, we made a passing mention of possible Node.js use cases. Now, we can explore this topic in detail. You know that the bulk of the technology’s popularity falls to the backend development. Frontend, as well as full stack usage of the tool, falls behind a bit. According to the latest survey made by Node.js Foundation, web applications are the top use case with the share of 85%. Taking into account all the strengths and weaknesses of this JS run-time environment, we composed a list of the hands-on solutions where you can leverage the technology.
For those out of step, RTA refers to a real-time app. I bet that most of you employ this type of applications on a daily basis. To name a few, Google Doc/Spreadsheets, as well as Slack, represent this use case. As a rule, collaborative services, project management tools, video/audio conferencing solutions and other RTAs require heavy input/output operations. Again, the asynchronous event-driven nature plus event API and websockets offered by Node.js ensure a seamless server operation (no hangup) and instant data update. Real-time chats are also tightly related to the technology, but they deserve a separate paragraph below.
This use case is the most typical RTA. Moreover, it is definitely a sweet-spot when we talk about Node.js implementation. If you aim at this type of product, you are likely to set such requirements as high traffic capacity, lightweight, as well as intense data flow. All these can be achieved in full using Node.js combined with some JS framework like Express.js on the backend. The already mentioned websockets play a key role in receiving/forwarding messages within the chat room environment.
Chat rooms are not much-in-demand independently except for their implementation as a component in online games. Node.js game development is another attractive use case. Actually, the combination of the technology with HTML5 and JS tooling (Socket.io, Express.js, etc.) allows you to construct RT browser games such as Ancient Beast, PaintWar, voxel shooting, Anagrammatix and many others.
Data streaming apps
Another product type where Node.js is used is a streaming app. The technology’s selling point is the ability to process data during the uploading time. Using it, you can transmit particular parts of the content and keep the connection open to download other components when necessary. In that context, Node.js streaming apps deal with not only video and audio data. Other forms are also available for input/output in real time.
Application programming interfaces (APIs) based on representational state transfer (REST) hold a fundamental position in building modern enterprise software architectures. The reason is a wide usage of the HTTP protocol. Besides, REST APIs are in demand in view of a trend towards microservices design patterns. Node.js ecosystem offers Express.js framework to build the lightweight and fast REST APIs. As for the benefits compared to other technologies – simple exposure of JSON objects with a REST API and no worries about conversion between JSON and MongoDB (with other databases that do not store data using JSON like PostgreSQL, transformation is necessary).
Server-side web apps
Express.js can complement Node.js for building web apps on the server side. Of course, it is worth mentioning that no CPU-heavy operations should be expected. Besides, a server-side web app is not an accustomed Node.js use-case.
Command line tools
This use case rests upon the Node.js’ aptitude for writing command-line scripts. On the web, there are plenty of tutorials on building hands-on examples. The technology’s expansive ecosystem is always an advantage, and you will easily find the right packages to make your CLI app.
Hardware programming is another answer to the question “What does Node.js do?”. The hardware includes robots, quadcopters, various embedded devices and the Internet of things (IoT). IoT can get the most out of Node.js on the server to process numerous simultaneous requests sent by lots and lots of peripheral devices. The JS run-time environment is a kind of interlayer between devices and DBs, and its asynchronous event-driven architecture enables a fast data flow.
What is NOT the best purpose of Node.js?
Considering the use cases described above, you may think that this run-time environment is a silver bullet for any project or idea. I wish! Unfortunately, there are cases when it is better to opt for Ruby on Rails or another technology instead.
CPU-heavy server-side computation
We’ve already said that heavy computations are not a strength of the technology. CPU-intensive operations jeopardize the blockage of the incoming requests and pushing the thread into number-crunching. Thus, all throughput benefits Node.js offers will fall into oblivion.
A CRUD app model refers to 4 functionality types (Create, Read, Update, Delete) implemented in apps with a relational database. When your goal is a simple CRUD app unencumbered with a separate API with a direct-from-server data route, Node.js might be a more-than-enough solution. On the other hand, to build a server for gathering analytical events, the JS run-time environment technology will be a perfect fit due to numerous parallel requests regardless of the type of DB is used.
Who uses Node.js
Not only independent developers and small dev teams choose the technology for their needs. Providing long-term support, Node.js attracts big companies as well. According to 2018 Node.js User Survey Report, the number of websites built with the tool exceeds 80K, and companies that use Node.js include IBM, Sony, SkyCatch, Uber, PayPal, SAP, and many many others. Here you will find some interesting app examples created with the technology.
Traditionally, the US is ahead of the pack by international presence (26%) of the technology. The second country in the list of Node.js users is India (10%) followed by Germany and Canada (6%) with a little lag. Globalization of users is expressed by the total number of countries (over 100) they reside in and languages (over 60) they speak. In that context, Europe is the leading hangout of Node.js developers so far.
Most Node.js users opt for Amazon Web Services (AWS) to deploy their products. Its competitors represented by Heroku, Google Cloud, and Digital Ocean Railsware are lagging behind not only AWS but also the on-premise infrastructure deployment, which experiences growth due to the rising Node.js popularity among big companies. Railsware, in turn, has posted our review of the best hosting services.
According to statistics, three in four engineers that employ this tech stack go into backend or full stack development. At the same time, along with the thumping majority of web apps built with the JS run-time environment, there are many other options for how to use Node.js in the digital world. Some of them you have discovered in this article, while even more information can be drawn out of books. Your next or current project, whether it is involved in programming robots/drones/devices or building a complex single page/real-time/data streaming app or even a huge IoT system will benefit from this tech stack. Eventually, you’ll ask yourself “Why not use Node.js?”, and take it for a spin.