blog post banner image

Why Use Nest.js

Ann McArthur

7/16/2023

The benefits of using Nest.js

NestJS is a framework that simplifies development. NestJS has its own rules and is not only for backend development. The framework is DDD-friendly, Event sourced, and offers microservice architecture. It is lightweight, simple, and open source.

NestJS is a relatively new option in backend development, with many features for building and deploying enterprise services quickly and following the principles of SOLID and 12-factor applications.

What is Nest.js?

NestJS is a progressive Node.js framework that helps build server-side applications. Nest extends Node.js frameworks like Express or Fastify adding modular organization and a wide range of other libraries to take care of repetitive tasks.

It’s open-source, uses TypeScript, and is a very versatile Node.js framework for building backend systems. It helps scale Node.js servers and supports several databases such as MongoDB, Redis, and Apache Cassandra.

Three Main Components

There are 3 main components of NestJS; controllers, providers, and modules. 

1️⃣  Controllers:

When an HTTP request is received, the routing mechanism directs the request to the appropriate controller. The controller then handles the request and returns the appropriate response.

2️⃣  Providers:

Providers are fundamental to Nest — a provider can be injected as a dependency to create relationships between various objects. Classes such as services, repositories, or helpers can be treated as providers simply by adding Nest's `@Injectable()` decorator. Providers can be injected into a class through the constructor, and Nest will handle resolving the dependencies, making dependency management extremely easy.

3️⃣  Modules: 

A module is intended to encapsulate a closely related set of capabilities. Each application has a root module, which Nest uses as a starting point to resolve the application's structure and relationships. It's highly recommended to use multiple modules to organize your application components. A module is defined using Nest's `@Module()` decorator, which describes properties such as the module's controllers, providers, and dependencies.

Easy to Test, Scale and Maintain

NestJS was created by Kamil Myśliwiec as a progressive Node.js framework to help with the development of scalable server-side applications. In recent years, it has gained increasing popularity with API development reaching 1 million downloads in early October 2021. The open-source NestJS app was inspired by Angular and is a tool that enables developers to build applications that are easy to test, scale, and maintain to increase their productivity.

NestJS vs. TSOA

When building DevCycle, our engineering team was looking at options for a new framework that had TypeScript support, built-in request validation, and documentation generated based on code. 

Originally they had started working with a library called TSOA because those three things are their bread and butter. TSOA is a framework that compiles with OpenAPI and has integrated type checking with TypeScript.  At first, using TSOA was great, especially since the engineering team was coming from the use of Express (TSOA and NestJS are built on top of Express), but it didn't take long for them to start running into problems and errors that we couldn't solve with TSOA.  

One of the things that made TSOA really easy to get started with is that it infers validation rules and documentation specs from our TypeScript types, which means in theory if they define their types well TSOA will handle the rest. In practice, once developers start creating more complex types the generated documentation started getting really messy, they had to start finding workarounds which meant sacrificing clean code in order to have clean documentation. 

Why Use It for Your Next Project?

The reason our engineering team chose NestJS is because the NestJS framework checked their three key boxes, is highly customizable, has extensive documentation, and has a broad community of followers. At a glance setting up an API with the TSOA appears fairly similar, however, the benefit of using Nest is that you can define your types, validation rules, and documentation independently but still within the same block of code.  

NestJS had better TypeScript support and did the documentation for our engineering team. It was a more mature programming framework that allowed our engineers to easily have type definitions in our APIs. In addition, with APIs, our team liked exposing a new entity than they have in their model, and NestJS was good for class layers.

A few other things we like about Nest applications is that the framework is more opinionated than Express, another Node.js framework, or TSOA, which is good for a growing team. Nest offers a lot of flexibility with its Guards and custom Decorators, plus they have more context than basic middleware. Finally, with Nest decorators, our engineers can define our MongoDB schema alongside our types which preserves compatibility.

If you’re looking to get started using feature flags to ensure software development best practices, start your free trial account with DevCycle.


Written By

Ann McArthur