Skip to main content

Introduction to NestJS

NestJS is a progressive Node.js framework designed for building efficient, reliable, and scalable server-side (backend) applications. It is built with TypeScript natively and combines elements of Object-Oriented Programming (OOP), Functional Programming (FP), and Functional Reactive Programming (FRP).


1. Conceptual Foundations and Architecture

NestJS provides an out-of-the-box application architecture that enables developers and teams to create highly testable, scalable, loosely coupled, and easily maintainable applications. It draws heavy inspiration from proven design patterns in enterprise development, notably Dependency Injection (DI) and modular layered architecture (similar to Angular or Spring Boot).

Request Lifecycle

In NestJS, incoming HTTP requests travel through an ordered sequence of stages before a response is returned to the client. Each stage fulfills a dedicated responsibility for security, validation, orchestration, or error handling:

NestJS HTTP Request Lifecycle

Stages of the Request Lifecycle:

  1. Client (HTTP Request): The client (browser, mobile application, or Postman) initiates a request containing HTTP method, headers, query/route parameters, and body payload.
  2. Middlewares: Execute first with direct access to req and res. Typically used for access logging, cookie parsing, and CORS header configuration.
  3. Guards: Determine whether a request is authorized to proceed by evaluating JWT bearer tokens, active sessions, or role-based permissions (RBAC).
  4. Interceptors (Pre): Trigger before route handlers execute to bind execution context, initialize audit logs, or start performance timers.
  5. Pipes: Validate and transform incoming payload data according to Data Transfer Object (DTO) schemas (e.g., validating required fields or casting with ParseIntPipe).
  6. Controller and Service: The controller matches the route endpoint (@Get, @Post) and delegates business operations to injected services where business rules and persistence logic reside.
  7. Interceptors (Post): Intercept the value returned by the service to mutate, wrap, or reformat the final response payload (e.g., standardizing responses into { data: ... }).
  8. Exception Filters: If an unhandled error occurs anywhere across the pipeline, exception filters capture and serialize the error into a clean HTTP response (400, 404, 500).
  9. HTTP Response: The server delivers the HTTP status code and final JSON response back to the client.

2. Visual Resources and Cheatsheet

Below you can explore the interactive module presentation and the official cheatsheet detailing key NestJS commands and architectural guidelines.



3. Self-Assessment Quiz

Cargando cuestionario...