What is Node.js used for?

When Netflix was optimizing its streaming platform with a backend written in Java, they decided to give a chance to an upcoming star: Node.js. This light, modern, technology allowed them to drastically improve performance. The reduction of the entire server application startup time from 40 minutes to just 1 on such a VOD platform demonstrates its game-changing abilities perfectly.

What is Node.js used for?
Contributor Photo - Kacper Geisheimer

Kacper Geisheimer

JavaScript Developer

As you can guess, there must be a reason why the tech giants have begun using this open source, community-loved engine. The most important one is that Node.js uses the popular Chrome JavaScript V8. In an era of JS front-end frameworks, it allows us to use the same language for both front and back-end. The potential of Node can be used not only for server-side and web development. Being modular, highly scalable and accessible via the CLI (Command Line Interface) on each platform gives Node.js its various use cases.

The key question is, how is it possible? There are many factors affecting Node’s ability to perform in this way.

Node.js is single-threaded and runs asynchronously in one event loop. The secret lies in using the power of non-blocking I/O architecture. This means that you should use asynchronous code (running in the background) as much as possible. This incredible power can be used with data streams.

Perfect bidirectional real-time data (also audio and video) streaming is often the strongest reason to choose Node.js over other technologies. Single-threading is not an issue (Node is able to run statelessly in a cluster using, e.g. the PM2 library) but unfortunately, it doesn’t handle big data processing well.

Let’s look at the most popular use cases for Node.js.

Event loop diagram - the heart of the Node.js V8 engine

Event loop diagram - the heart of the Node.js V8 engine.

Node.js and Microservices

This choice of name wasn’t a random decision. The full potential of its architecture is utilized by building applications as small nodes (parts). Does that sound familiar? Microservices are apps that need to be lightweight, blazing fast, scalable and stateless. Deploying thousands of small (but powerful) nodes in clusters and daemon is available mostly out-of-the-box with tools like PM2.

You might be also interested in the article:

How to use JavaScript Classes? Three Different Ways

How to use JavaScript Classes? Three Different Ways

Well, Node is light and tiny, but what about connecting to other services? Thanks to the npm package manager, you can use all of its content, including web servers, databases connectors, REST & GraphQL implementations and instant integrations with various API providers, such as AWS.

Web building has probably never been so easy as it is with the express framework. Nowadays, a complete web server could be written in a couple of lines. Node.js is also intended to be fast in use - a ready application can be deployed to zero-configuration Heroku servers with a line of CLI command.

Breaking down an app into smaller ones also overlaps with Kubernetes, the service that facilitates automation and configuration of containerized applications. You can compare it to Lego blocks. With a decent amount of small bricks, you can create complex, effective and stable results. Due to the nature of their simplicity, Node.js applications offer the high availability needed in production environments.

The simple architecture of an application built in Node.js:

 The simple architecture of an application built in Node.js

Real-time communication in Node.js

Building low-latency live servers with opened bidirectional connections is a weak point of many technologies. In some of them, e.g. PHP which runs per request and not as a still opened socket, it’s just really hard to achieve due to the architecture.

Of course, it’s possible to build it in other languages (e.g. Python, Java), but that still requires a lot of effort and workload. Then comes the time for implementing the connection on the front-end. Fighting with discrepancies of encoding, modes, and protocols are not something we like to spend time on. Is there another, developer-friendly solution?

Of course, there is. One of the great things about Node is its scalability. When you don’t need to have tons of individual settings, just download an npm package and use it with a few lines of code. It’s the same with sockets. In this case, let’s use socket.io as an example. It’s an open source version of various WebSocket implementations available both client-side and server-side. Sharing exactly the same code for all platforms, with automatic scaling and load-balancing across multiple machines, and just a crumb of code to work - everything out-of-box. There’s nothing more to say… Don’t hesitate to try it:

// Server
import http from 'http';
import express from 'express';
import SocketIO from 'socket.io-server';

const PORT = 80;

const app = express();
const server = http.Server(app);
const io = new SocketIO(server);

io.on('connection', (socket) => {
    socket.on('ping', () => {
        socket.emit('pong', { alert: 'This is a sample message.' });
    });
});

server.listen(PORT, () => {
    console.log(`Listening on *:${PORT}`);
});

// Client
import SocketIO from 'socket.io-client';

const io = SocketIO();

io.on('pong', (payload) => {
     window.alert(payload.alert);
});

io.emit('ping');

The library I describe above is used by one of the most popular PHP frameworks, Laravel. Laravel Echo was built on the top of Node.js + socket.io and is their flagship real-time communication module.

Prototypes & MVPs in Node.js

Fulfilling the client’s requirements when delivering a prototype or MVP usually puts team members under time pressure. When time matters and we don’t need pixel-perfect quality, spending that time on architecture and planning does not lead to success. Node.js, with its npm and thousands of ready-to-use packages, allows a team to focus on useful development.

Usually, at these stages of product development, Node.js can be used with boilerplate projects and default module configurations. Saving time by writing tiny, legible code and spending that saved time on individual needs is an excellent use case of Node.js.

You might be also interested in the article:

Marble.js – new open source framework for JavaScript

Marble.js – new open source framework for JavaScript

Even more benefits can be achieved with a front-end written in JS. All kinds of APIs (e.g. REST, GraphQL) do not require additional procedures, like transforming JSON-to-object and vice-versa. Accessing all kinds of data looks exactly the same, which gives the ability to reuse code in accordance with coding standards.

Let’s build a very simple REST API, mock up some predefined data from a .json file and share it via GET endpoint:

import express from 'express';
import loadedData from './mock.json';

const app = express();

app.get('/', (req, res) => res.json({ status: true, message: "Hello world", data: loadedData }));

app.post('/', (req, res) => res.json({ status: false, message: "Exception thrown! Check details argument.", details: "Place your details here" }));

app.listen(port);

Command line tools in Node.js

If you need to write a small CLI tool or a big console application, usually you would use C++. This language has been used for this kind of projects for years. But it requires a lot of time and skill, and the outcome is not always an adequate reward for the necessary effort. And here comes the Node.js part! The Node engine offers direct access to most of the useful functions and API layers for I/O or network operations. The lack of needed parts could be filled with repo resources from npm packages.

After publishing to npm, CLI packages are able to download with -g flag. They can be used globally as standard executables built for their operating system. There is no need to add the package to PATH, as it is automatically saved to the node installation directory.

Comparing C++ and Node.js, the first is better when it comes to performance, but the writing is surely more time-consuming. So, if you prefer a more friendly environment over performance, use Node.js. It is worth adding, that you can cross-use C++ binaries with Node tools, which makes things much easier.

Data scraping & automation in Node.js

Unfortunately, not all website owners provide transparent, ready-to-use APIs. Manually copying and processing data periodically wastes a lot of time and is just annoying. The fact is that websites are built with HTML and JS. The first of mentioned of these is simple, structured data, which can be parsed by almost any programming language into its native objects. What about JavaScript? Well, we can manually recreate logical steps based on its code, but is it worthwhile? With Node, we have direct access to read, manipulate and execute JavaScript code in the browser.

The biggest advantage of Node.js, when it comes to data scraping, is that you can test your code directly in any browser.

The libraries and ecosystem of Node.js offer the possibility of accessing a browser as a user, not a dev or machine. An entire DOM built with HTML can be crawled with standard jQuery syntax using the Cheerio library. Adding other modules (e.g. network communication, like request) enables the building of a complex scraping and automation structure. If you want to save time on repeatable steps and just focus on unique functionalities. Voilà!

Let’s load the Google.com site and grab the alternative text for its logo, which is equal to the name of the current international day:

import request from 'request';
import cheerio from 'cheerio';

const url = 'https://google.com/';

request(url, (err, res, body) => {
    if (err) {
        return console.error(err);
    }

    const $ = cheerio.load(body);
    const dayName = $('#hplogo').attribs.alt;

    console.log(`Today we’re celebrating ${dayName}`);
});

Is that all?

Of course not! Due to its flexibility and wide variety of open source packages, Node.js has almost the unlimited ability to make your development dreams come true. With its high level of scalability, it can be used with applications of various sizes and needs. From small utility tools to big, complex web servers, Node.js is always lightweight and developer-friendly. Long story short, Node.js is that type of technology which just works without unnecessary effort. Yes, it may have some disadvantages (e.g. big data computing) but it is still worth giving it a try.

Contact:

Let's talk about
your business