NodeJS — What, Why, How?

Petar Stevovski
Codeart
Published in
8 min readJul 9, 2021

What is NodeJS?

NodeJS is an open-source, cross-platform, single-threaded JavaScript runtime environment, that is used for executing JavaScript code outside the browser. It uses Google Chrome’s popular V8 engine and an event-driven, non-blocking I/O model, which makes it very performant.

Before NodeJS, JavaScript applications could only run in the browser. Every browser has its own implementation of a JavaScript engine, which takes the code written in JavaScript, and converts it to machine code, that the computer can actually read and understand. So basically, every browser provides a runtime environment for running JavaScript. Then, in 2009, Ryan Dahl took Google Chrome’s JavaScript engine (V8) and embedded it into a C++ program. The result of that is — NodeJS.

NodeJS is ideal for building superfast, highly scalable, data-intensive, and real-time applications, and it is used in production by large companies such as PayPal and Netflix. As a matter of fact, PayPal rebuilt one of its Java & Spring-based applications, with NodeJS, which resulted in:

  • It was built twice as fast with fewer people than it took for the Java & Spring version
  • 33% fewer lines of code and 40% fewer files
  • Doubled the requests served, per second
  • Significantly decreased response time, by up to 35%
Source: PayPal

What NodeJS is NOT?

NodeJS is NOT a programming language. It also is NOT a JavaScript framework.

Instead, NodeJS is a runtime environment that allows us to run JavaScript code outside the browser. Any comparison made between NodeJS and other languages (such as Java, C#, Python, PHP, etc.) or frameworks (Spring, ASP.NET, Django, Laravel, etc.) is fundamentally wrong.

Why use NodeJS?

Apart from being super fast and highly scalable, especially suitable for I/O applications, there are a few other benefits when using NodeJS:

  • Great for prototyping
  • JavaScript everywhere, so frontend developers who are good at JavaScript can now also work on the backend
  • Results in a cleaner and more consistent codebase, using the same tools, naming conventions & best practices across frontend and backend code
  • Has the largest ecosystem of open-source libraries available to use through its package manager npm, although that sometimes can be a “two-edged sword”, for example, when a legal dispute over 11 lines of code, caused issues with many packages, including React, Babel and many more.

How NodeJS works?

NodeJS is non-blocking or asynchronous by default, without the need for extra work. We have a single thread that is used to handle all of our requests. So, when a request arrives, that single thread is used to handle that request, for example, to query our database.

Now, because querying the database might take some time to finish, our thread does not have to wait for the database response to continue. Instead, the same thread, after handling the previous request, can now be used to handle another request, while we wait for the response from the previous one.

When the data from the database is returned (as in our example), a message is emitted to the event queue which is monitored by Node, all the time in the background. When Node finds an event in this queue, meaning that we’ve got some response, it will take it out of the queue, and process it.

Source: Programming with Mosh

On the other hand, just for comparison, applications written in ASP.NET, Rails, etc. are by default blocking or synchronous, meaning some extra work will be needed to implement synchronicity.

When such applications handle a request, the thread responsible for handling it is blocked - it’s waiting for the response to come back (e.g. querying a database), so it can process it, before continuing onto the next request, which means that this thread can’t serve another request until it has finished handling the previous one.

If a large number of concurrent requests happens, we might run out of free threads to handle those requests, so new requests will have to wait until some of the threads are available, or.. we could add more hardware (which can get quite expensive).

Source: Programming with Mosh

Because of this difference in the architecture, Node is ideal for building intensive I/O applications, that include a lot of disks or network access, because we can serve more requests without the need to throw in more hardware. That makes Node highly scalable.

Of course, all this comes at a cost. Since Node (& JavaScript itself) is single-threaded, it should not be used for CPU-intensive applications such as video editing, image manipulation, etc. In these kinds of applications, there are a lot of calculations that the CPU needs to do, and because of the single-threaded nature, it will result in blocking all requests and putting them on hold, until the ongoing request has been fully processed and completed.

Types of projects NodeJS can be used for

NodeJS can be used for many things, including:

  • Package management through npm, yarn, etc.
  • Development tools such as module management (webpack), code linting (eslint), task running, and automation (grunt and gulp)
  • Backend web applications including REST and GraphQL APIs, microservices, streaming, real-time web applications such as chats, etc.
  • CLI (command-line interface) applications such as create-react-app

However, it is mostly used in combination with frontend JavaScript and frameworks, for building full-stack web applications.

NodeJS Frameworks

There are a couple of NodeJS frameworks that help with the development of backend web applications, such as:

  • ExpressJS - the most popular, minimal, and flexible NodeJS web application framework, offering great API and performance
  • Nest - a framework for building efficient, scalable NodeJS server-side applications, which under the hood uses Express
  • Adonis - an MVC framework that offers a stable ecosystem
  • Meteor - full-stack JavaScript platform for developing modern web and mobile applications
  • Koa - a new web framework designed by the team that developed Express, with the aim to be a smaller, more expressive, and robust foundation for web applications and APIs

ExpressJS

The most popular and most used NodeJS framework today, by far, is undeniably ExpressJS. This framework is the backbone on which many new frameworks are being based off. It offers great API, performance, and stability.

It is a server-side, or backend, framework, which is not in any way comparable to frontend frameworks such as React, Vue, Angular, Svelte, etc. But it is widely used in combination with these frontend frameworks for building full-stack applications. There are a few differentiating stack combinations that are based on the selected frontend framework: MERN, MEAN & MEVN (MongoDB, Express, React/Angular/Vue, NodeJS).

It is clear that MongoDB is the most commonly used database when working with NodeJS & Express, but that doesn’t mean that you can’t use other databases (both relational and non-relational). All you need to do to connect a database to your Express application is to use the appropriate database driver, such as Mongoose for MongoDB. Or, you could also make use of more complex ORMs, such as Sequelize or TypeORM, that offer support for both relational and non-relational databases.

Starting a NodeJS / Express server is very simple:

import express from "express";// Initialize express
const app = express();
// Define the initial route
app.get("/", (req, res) => {
res.send("<h1>Hello World!</h1>");
});
// Define the port to which the server should connect
const PORT = process.env.PORT || 3000;
// Start and connect to the server
app.listen(PORT, () => {
console.log(`Server is running at PORT: ${PORT}.`);
});

If we then open http://localhost:3000/ we’ll get a Hello World! message in the browser.

Project Structure

Since ExpressJS is an unopinionated framework, it doesn’t give us a strict structure to follow as a “rule of thumb” when we’re creating our projects. That can sometimes be an issue, especially for someone that is just starting.

A project structure that I personally follow when creating NodeJS / Express projects, using TypeScript and MongoDB, is:

Project structure overview

This provides an easy overview, good organization, and a clear distinction of the responsibilities of the code.

Generate your NodeJS / Express project

To simplify the process of creating a NodeJS / Express project, like defining the structure, installing necessary packages, setting up connections, etc., I have created a simple CLI program that helps you generate your project fast and easy, without all the setup hassle.

You can check it out here: express-app-cli.

Example usage of express-app-cli

If you’re using MongoDB as a database, then all you’ll have to do, after generating the project, is to call npm run watch and you’re good to go.

If you’re using a relational database, you’ll also need to setup the connection settings in the .env file, such as your database name, username, and password.

Deno - an alternative to NodeJS?

Deno is a JavaScript/TypeScript runtime, with security in its focus, created by the same creator of NodeJS (Ryan Dahl).

Some of the key features highlighting Deno include:

  • Secure by default as it has no access to the file system, network, environment, etc. unless explicitly enabled
  • Supports TypeScript out of the box
  • Ships a single executable
  • Has a bunch of built-in utilities
  • Has a set of reviewed standard modules that are guaranteed to work with Deno

Starting up a server with Deno can be as simple as:

// deno-example.ts
import { serve } from "https://deno.land/std@0.83.0/http/server.ts"
const server = serve({
hostname: "localhost",
port: 3000
});
console.log("HTTP Server is running. Access it at: http://localhost:3000/");

which can then be run using the deno run --allow-net deno-example.ts command.

Summary

NodeJS is the “go-to” option for developers that already know JavaScript from using it on the frontend, and want to dive into creating web applications, CLIs, development tools, etc. It is fast, stable, and a very popular choice nowadays. Even though it is not perfect, as Ryan Dahl mentions himself, still, due to its huge ecosystem, community, and simply the fact that NodeJS has been around for a while now (11 years), it won’t go anywhere in the near future, so it’s safe to say that you should learn and start using NodeJS.

--

--