Why use Node.js in your next project?

Dec 10, 2020 · 10 min read

Imagine you have a web application idea and it looks great. Still, you should validate it before investing tons of time and money in its development. It would be good if one full-stack programmer could quickly build an MVP for you to test it on the market. And this is when Node.js comes in handy.

With Node.js, the whole web app can be built using a single programming language – JavaScript – whereas normally two different languages are used for developing the frontend (JavaScript) and backend (Ruby, Java, or PHP) of a web app.

Prototyping both client and server parts of a web app in JavaScript shortens the MVP development cycle and saves resources. Although this is the most common Node.js use case, there’s more. Let’s see what else Node.js is used for and why you should consider having it on your tech stack.

Tech stack that Node.js brings to the table

Node.js is a back-end JavaScript run-time environment that allows executing JavaScript code outside a web browser (on a server). It leverages Google's V8 JavaScript engine and libuv C++ cross-platform abstraction library that makes it super fast.

Node.js has a powerful ecosystem that includes back-end frameworks, such as Express.js for HTTP servers and Socket.IO for WebSocket servers, and a bunch of other helpful tools that every Node.js developer uses Webpack, Babel, and MongoDB to name a few.

What’s more, Node.js is open-source and free-of-charge. It’s being continuously enhanced by an active community and goes with thousands of reusable code packages available on the Node Package Manager (nmp) resource.

Event-driven, non-blocking, and asynchronous

The single-threaded and event-driven architecture make Node.js the best tool to solve the I/O scaling problem but the worst tool to solve the compute scaling problem. It’s caused by the way Node.js works.

The computation on the Node.js backend is triggered by events. Those may be an HTTP request, a message coming over a socket, a user’s input from a keyboard, a request generated by an IoT device sensor, etc. Node.js server handles all of those using an event loop that makes requests to event providers and calls relevant event handlers.

Node.js has two threads: an Event Loop for taks orchestration and a Worker Pool for task execution. It shares those two threads among all clients. Node.js enqueues all incoming requests and processes them one by one. If none of the requests requires heavy computation, they go through the Event Loop or the Worker Pool very fast allowing the app to hold thousands of concurring connections.

If the Node.js server receives a computation-intensive request, it takes too much time to process. In this case, the thread will block all subsequent incoming requests until this computation is completed.

Maintaining a single-threaded process requires the Node.js server to offload all CPU-intensive tasks to third instances (other apps, servers, or microservices) and focus on orchestrating the data exchange between them. For this, it uses non-blocking I/O calls that have callback functions and executes them asynchronously.

Asynchronous execution allows the Node.js server to receive a request from a client, call the third instance that will accomplish this task, and move to the next client request immediately. When the task is completed, the third instance sends a callback to the Node.js server. The callback joins the end of the queue and waits until the Node.js server processes it and sends a reply to the client.

Due to the specifics and limitations of its architecture, Node.js is not a one-size-fits-all solution. That being said it has a broad portfolio of cases where it truly shines.

Not a silver bullet but feels a particular need

For the last decade, Node.js has proved to be a perfect technology for numerous startups and enterprise-level apps. The remarkable number and variety of Node.js open-source modules contributed to the community prove the health of the technology.

According to the annual Node.js User Survey Report, in 85% of cases, Node.js is used by full-stack and JavaScript programmers to create web apps. However, Node.js has been implemented in other domains – from apps with microservice architecture to IoT programming. Let’s go through some common Node.js use cases and problems it solves to see what Node.js can do for your particular project.

Node.js is good for service-oriented architecture

An app with microservice architecture represents an assembly of small interconnected services each executing a small part of its business logic. Services are independently deployable, scalable, and updatable, which results in a flexible development process. However, such a fragmented system requires extra effort to orchestrate and integrate all the services together.

Node.js is one of the favorite developers’ tools for service orchestration. Having high I/O performance, it speeds up request exchange between different microservices. Node’s Express.js framework jungles API and HTTP requests with a speed that is impossible in other microservice handling frameworks, such as Spring Boot (Java).

What’s more, Node.js contributes to the microservice app scalability. How fast an app scales depends on the start time of new service instances in response to increasing load. Node.js has a 15-times faster startup time than Spring Boot and remains unbeatable by this parameter.

One more thing that puts Node.js in line with microservices is the small amount of memory it occupies. This feature helps pay less for hosting, which improves apps’ cost-efficiency.

Using Node.js improves SEO for single-page apps (SPA)

When building SPAs developers write a great deal of JavaScript code because most of the rendering for the app’s single HTML page happens on the client-side. The code loads from the server once when the app starts. Then the dynamic page handles the actions of its users by re-rendering part of the page content in the browser without making the users wait for client-server communication to happen upon every click.

For SPAs, extending the use of JavaScript server-side makes great sense as they may work with a data-centric API backend. Node.js is ideal for this and allows creating the entire app on JavaScript with one full-stack developer within a short timeframe.

Another reason why to use Node.js for building a SPA is to improve its SEO-friendliness. You may need to implement server-side or hybrid rendering for page meta tags to change dynamically when another view is loaded on the SPA page. Node.js allows rendering meta tags server-side, which may help Google index your SPA correctly.

Node.js speeds up instant messages and real-time apps

Providing a backend for real-time, bi-directional, event-based communication using Sockets is something Node.js has a leg up. Developers can implement time-tested solutions for instant messaging and chats available on the npm resource, which minimizes development time for chat apps.

Socket.io is a Node.js module that is used for this purpose most often. It’s universal and works reliably on every platform, browser, and device. The Socket.io module listens to events emitted by any connected client, such as change of the user name, new message, or disconnect, and broadcasts them to all other connected clients.

Online collaboration tools for project management or collaborative documents editing represent another great use case for the Node.js real-time engine. Node’s ability to handle I/O-intensive tasks and its high scalability ensure the collaborative environment works fast and responds instantly to all users’ actions.

Node.js is used for creating data streaming apps

Node.js can power data streaming apps using Streams. This data-handling method is aimed at efficient end-to-end information exchange. Instead of reading the whole file (video or audio) in memory, it divides the data into chunks and processes them one by one.

It means Node’s Streams improve the time efficiency of the streaming service by allowing a client to start reproducing a file while it’s being downloaded.

Streams in NodeJS provide for memory efficiency as they don’t require caching and storing temporary data. This technology enables users to read files that are larger than the available memory volume.

Node.js is perfect for the Internet of things (IoT)

Having to process numerous simultaneous requests from peripheral devices, IoT apps can be as I/O-intensive as real-time apps. This means they are a natural fit for Node.js that supports Sockets and leverages Streams.

Being lightweight, the Node.js runtime environment suits not only the server but also the device part of the IoT app. It can easily be installed on controllers as a programming environment. In addition, it allows programmers to add C++ modules and use C drivers written for the devices.

For a faster development process, IoT developers can reuse numerous nmp modules written for Raspberry Pi, Intel IoT Edison, and Arduino controllers, as well as sensors, motors, and Bluetooth devices. The most popular IoT modules are Johnny-Five, NodeRed, Noduino, Nodebots to name a few.

Building a Node.js web app for 3 months

We asked Alexey Kaplun, Frontend Developer on Proxify, about his experience of using Node for building fullstack JavaScript web apps. Recently Alexey developed a marketing web application and integrated it with several Amazon services. His team used React for the frontend, Node.js for the API backend server, and the PostgreSQL database.

“For us using JavaScript on both client and server parts of the app was an undoubted advantage. We had 2 fullstack JS developers, who were able to complete tasks involving both backend and frontend.” – Alexey Kaplun, Frontend Developer

Although the project wasn’t that simple, the two-man JS team moved through their tasks very fast. They didn’t have to spend time on negotiations between frontend and backend developers as they would if they had a different server-side programming language. As a result, the whole project took them three months to complete.

“Node.js server performed well even without any additional software or external services that are commonly used to improve performance.” – Alexey Kaplun, Frontend Developer

The application required them to solve several challenges. They had to develop a range of complex database queries that involved transactions and blocked access to particular database tables until being completed. Also, they had to implement a bunch of API requests to external services with asynchronous callbacks.

“I won’t tell you that using Node.js server-side is a piece of cake. For the success of a project, developers should be aware of some peculiarities and rules of coding. For example, Node.js is not good for math or CPU-intensive tasks, but it is very good for I/O handling. I should also admit that using Node.js on the server of a high-loaded project with a complicated business logic may be challenging. So far, it’s impossible to scale load on multiple CPUs, or support multiple threads as Node.js runs a single-threaded event loop.” – Alexey Kaplun, Frontend Developer

Alexey believes Node.js is an ideal tool for building MVPs and tiny services. It allows implementing small projects with simple architecture in no time. However, it doesn’t mean you can’t use it for complex tasks, it just would require hiring highly-skilled developers to solve nontrivial tasks.

Looking for Node.js experts?

The demand for specialists having Node.js on the list of their tech skills is quite high. So their hourly rates are. At the same time, it’s critical to hire Node.js developers with solid expertise. You should be sure they can write high-quality and secure code, structure complex and scalable architectures, and ensure perfect app performance. On Proxify.io you’ll find senior developers who meet the highest requirements. Just send us your talent request, and we’ll match you with the ideal candidate within two weeks.