We know JavaScript as a language to write once and run anywhere. It began its ascension with browsers, where JS became a standard language for manipulating web pages. Thereafter, it moved to the server side and established itself on web servers. That entailed a capability to generate web pages on the server.
However, JavaScript’s first run at the backend was short-lived and has probably vanished from the developer community’s memory. As we go along, we’ll explore numerous types of JS employment, like writing a command line app or a specific search engine. Still, to build a general-purpose application using JS was a challenge. The wind of change blew when Node.js came out.
Node.js is not a JS framework
There are many web frameworks with underlying JavaScript. These include Angular and React, Meteor.js, Vue.js, and others. All of them contribute to the development process by increasing efficiency, safety, and cost-effectiveness. Although Node.js allows you to build platform-independent web apps, it is not a JS framework. The official description or title of this tool is a run-time environment which, in turn, means a bigger scope of implementation. On that account, uses of Node.js are not limited to web applications but also include microcontrollers, REST APIs, static file servers, OS wrappers, robots, and even drone programming. Instead of a listless request-reply message exchange pattern, the technology applies a progressive event-driven paradigm, which provides an event-loop ready to react to an event.
How it works
The essence of the technology is a highly customizable server engine that uses non-blocking event-based input/output model. It makes a kind of translation of JS into machine language which provides increased performance and agility. As a result, we get a run-time environment, where JS code moves fast in the server-to-client direction. With Node.js, JavaScript increased its capabilities from just building interactive websites to a broader scope of use cases that we’ll review later.
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.
Pros | Cons |
---|---|
– Full stack JavaScript development 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/outputWith 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 engineThough 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 architectureIn 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 ecosystemThe 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 an 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 hellThis 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.
Complex SPAs
A single-page app (SPA) involves the allocation of an entire application on one page, whereas the UX is akin to a desktop application. This type of product is popular for building online text/drawing tools, social networking or mail solutions, and numerous versatile websites. In that case, Node.js app development is a good fit for making SPAs due to its asynchronous data flow on the backend. The event loop “catches” simultaneous client’s requests which provides a smooth data update. In practice, it eliminates the necessity of refreshing the page every time to get new data. Besides, a bunch of SPAs have been created with different JS frameworks/libraries including React, Meteor, Vue.js, Angular, etc. JavaScript is a common language between these tools and Node.js which improves the development process by reusability of structures and approaches both on the frontend and backend.
RTAs
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.
Chat rooms
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.
Browser Games
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.
REST APIs
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
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.
Robotics is also an attractive area, which is now open for those having basic JavaScript knowledge. With Node.js and the appropriate frameworks (Johnny-Five, Cylon.js), you have opportunity to delve into programming robots and JS-controlled devices like NodeBots.
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.
CRUD apps
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. Check out reputable Node.js app examples we’ve blogged about.
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 for Node.js.
Bottom Line
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 a 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 the best Node.js 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.