Skip to content

Introduction to NestJS

NestJS is a progressive Node.js framework for building efficient, scalable server-side applications. This introduction explains what NestJS is, why it exists, and how it differs from lower-level frameworks like Express.

What Is NestJS?

NestJS is a TypeScript-first framework for building server-side applications on top of Node.js. Created by Kamil Myśliwiec and first released in 2017, it borrows architectural ideas from Angular, modules, dependency injection, and decorators, to bring structure and testability to backend code.

Under the hood, NestJS does not reinvent HTTP handling. By default it runs on top of Express (and can optionally run on Fastify), so everything you already know about middleware and the request/response cycle still applies, wrapped in an opinionated, modular architecture.

import { Controller, Get } from '@nestjs/common';

@Controller('cats')
export class CatsController {
  @Get()
  findAll(): string {
    return 'This action returns all cats';
  }
}

A NestJS controller looks similar to an Express route handler, but the routing and HTTP method are declared with decorators (@Controller, @Get) instead of imperative calls like app.get().

How a NestJS Request Flows

Request in
   -> Middleware
   -> Guards
   -> Interceptors (pre-controller)
   -> Pipes
   -> Route Handler (Controller -> Service)
   -> Interceptors (post-controller)
   -> Exception Filters (on error)
Response out
  • NestJS applications are built from modules, each grouping related controllers and providers.
  • Controllers handle incoming requests; providers (usually services) contain the actual business logic.
  • Dependency injection wires controllers, services, and other providers together automatically.
  • The request pipeline layers guards, interceptors, pipes, and exception filters around your route handlers.

NestJS Core Building Blocks

The pieces you will meet in almost every lesson throughout this course.

Concept Decorator / Class Purpose
Module @Module() Groups related controllers and providers
Controller @Controller() Handles incoming HTTP requests
Provider @Injectable() Encapsulates business logic, injected via DI
Route @Get() / @Post() Maps an HTTP method and path to a handler
Pipe PipeTransform Transforms and validates input data
Guard CanActivate Controls whether a request is allowed to proceed

Why NestJS Was Created

Express and Fastify are intentionally unopinionated, they don't dictate how to organize files, structure business logic, or test an application. As Node.js backends grew larger, teams found themselves reinventing the same architectural patterns, modules, DI containers, layered services, project after project.

NestJS packages those patterns into the framework itself, borrowing heavily from Angular's module system and dependency injection container, while still running on the same battle-tested HTTP layer most Node.js developers already know.

  • Provides an opinionated, testable architecture out of the box.
  • Uses TypeScript and decorators for clear, self-documenting code.
  • Built-in dependency injection removes manual wiring of services.
  • Ships official integrations for GraphQL, WebSockets, and microservices.

Where NestJS Fits

NestJS is best understood as a structured layer on top of Express (or Fastify), not a replacement for it. You can access the underlying Express Request and Response objects at any time, and you can gradually adopt NestJS conventions inside an existing Node.js codebase.

Common Mistakes

  • Assuming NestJS replaces Express entirely, it runs on top of it by default.
  • Skipping TypeScript fundamentals before starting NestJS, since decorators and typing are central to the framework.
  • Writing all logic directly inside controllers instead of delegating to injectable services.

Key Takeaways

  • NestJS is a TypeScript-first, opinionated framework built on top of Express or Fastify.
  • It borrows modules, decorators, and dependency injection from Angular's architecture.
  • It adds structure without giving up direct access to the underlying HTTP layer.
  • Modules, controllers, and providers are the three building blocks you will use everywhere.

Pro Tip

Install the Nest CLI early (npm i -g @nestjs/cli) and generate a throwaway project with nest new sandbox just to click through the generated files, by the next lesson you'll already recognize modules, controllers, and services.