What Is NestJS?

Get introduced to NestJS and its features, tooling, and language.

Overview

NestJS is a progressive NodeJS framework for building efficient, scalable, and maintainable applications. NodeJS is a runtime environment that allows for the executing of JavaScript code on the server side. NestJS leverages the nonblocking, event-driven nature of NodeJS, making it highly efficient for building scalable applications.

NestJS is called a progressive framework because it allows developers to adopt and utilize its features gradually. For example, we can start a NestJS project with minimal setup, including the routing, essential endpoints, and services. As the project progresses, we can add more features gradually (i.e., authentication, database integration, real-time charts using WebSockets, and other advanced features within the NestJS ecosystem). With NestJS’s modular architecture, we can plug in new features without overhauling the project. In contrast, a nonprogressive framework often starts with a more comprehensive setup that includes a wide range of features. It isn’t as flexible for developers who might prefer to adopt features gradually based on the project’s needs.

NestJS has been widely adopted in the industry by notable companies like Roche, Adidas, Autodesk, GitLab, IBM, ByteDance, and many more. It’s one of the most popular NodeJS-based frameworks, with over 57,000 GitHub stars at the time of writing.

Opinionated framework

NestJS is an opinionated framework because it provides specific architecture patterns, conventions, and best practices. Inspired by Angular, NestJS uses a similar architecture to build server-side applications with modules, providers, middleware, and services. This similarity means that what we learn about NestJS can be readily applied to Angular projects and vice versa.

Note: Angular is an open-source JavaScript framework for building single-page applications (SPAs). Developed by Google, it’s one of the most feature-rich and popular front-end frameworks.

The opinionated nature of NestJS simplifies the decision-making process because NestJS offers a set of patterns, conventions, and best practices to follow. Its modular architecture and rich features allow us to focus on writing business logic and be more productive.

Note: Modular architecture means the code is organized into small, self-contained modules. Each module acts like a building block, responsible for a different task.

NestJS vs. Express

In contrast, Express is a non-opinionated framework. It doesn’t enforce a specific project structure, coding style, or architectural pattern, leaving many decisions up to the developer.

Express is a popular minimalist framework based on NodeJS, which means we need to wire everything manually (i.e., database access) with our own design and architecture.

NestJS is more developer-friendly and structured than Express. Below is an example:

Press + to interact
// NestJS
import { Controller, Get } from '@nestjs/common';
@Controller()
export class AppController {
@Get()
getHello(): string {
return 'Hello, World!';
}
}
// Express
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello, World!');
});

Here, NestJS uses decorators and a class-based approach, making the code more structured and easier to understand. In the Express example, the route and corresponding handlers are manually defined, leading to a relatively unstructured codebase. When more routes are added, the Express routes will be harder to read and maintain.

Features and tooling

NestJS comes with an array of powerful features and tools right out of the box, simplifying the development workflow. These features are described below:

  • Command line interface (CLI): NestJS provides a robust CLI that automates common development tasks, making it easier to scaffold, generate, and manage our project.

  • Dependency injection (DI): The framework boasts a built-in DI container, which promotes clean and maintainable code by facilitating the management of class dependencies. DI is a design pattern that allows us to pass dependencies, for example, services, into a component rather than having the component create them.

  • Testing integration: NestJS seamlessly integrates with testing frameworks like Jest, enabling us to write comprehensive unit and integration tests to ensure the application’s reliability.

  • TypeScript: NestJS is built with TypeScript from the ground up, leveraging TypeScript’s strong typing and modern features for safer and more maintainable code.

  • Extensive module ecosystem: NestJS offers a vast selection of modules to streamline integration with various technologies, including ORMs, GraphQL, logging systems, WebSockets, and more. This modular approach simplifies the inclusion of essential features and libraries into our project.

  • Documentation: NestJS prides itself on excellent documentation, making it easier for developers of all skill levels to grasp the framework’s concepts and features.

Press + to interact

Although NestJS uses Express as its underlying web server framework by default, its design allows us to change to other frameworks.

NestJS is built with TypeScript, a powerful language that offers benefits like static typing and a robust type checker. TypeScript helps catch errors early, ensuring robust code. While NestJS supports JavaScript, we’ll use TypeScript in this course due to its alignment with NestJS principles.

To summarize, NestJS provides a comprehensive set of features and tools to facilitate the development of server-side applications. These capabilities include managing and validating data, and they also provide authentication, configuration handling, robust testing support, and a growing ecosystem, making NestJS a versatile framework for building server-side solutions.