Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 42 ยท Getting Started

What is NestJS?

Learn what NestJS is, why it adds structure on top of Express or Fastify, when it beats plain Express, and see its core idea in a small runnable stand-in.

NestJS (usually just "Nest") is a framework for building server-side applications with Node.js and TypeScript. It does not replace Node's HTTP server. Instead, it sits on top of one, either Express or Fastify, and gives your code a fixed shape: controllers handle requests, providers hold business logic, and modules group related pieces together. The result is a back end where every developer on the team knows where to look for things.

Why does that matter? Express is happy to let you put anything anywhere. That freedom is great for a 50-line prototype and painful for a 50,000-line product with ten developers. Nest answers with conventions borrowed from frameworks like Angular, Spring and ASP.NET: dependency injection, decorators, a clear request pipeline and a first-class testing story. You write more declarations up front, and in return large codebases stay readable.

Our editor cannot install npm packages, and a real Nest app needs TypeScript, decorators and a build step. So in this tutorial the real Nest code is read-only: you see exactly what you would write, and we are careful about what it does. Every lesson also gives you runnable stand-ins, small plain-JavaScript programs that re-create the idea inside Nest so you can run it, change it and see how it works. We always say which part is real and which is the stand-in.

What Nest gives you

One framework, four big ideas

Modules Group related code into units.
Controllers Map HTTP requests to methods.
Dependency injection Nest builds and wires your classes.
Request pipeline Guards, pipes, interceptors, filters.

Each of these gets its own lesson in this tutorial.

1 / 3

Nest is not magic. Under the hood, a controller is a class that a router calls, and a provider is an object that a container creates once and hands to whoever asks. Once you see those two mechanisms, the rest of Nest is easy to follow.

What a Nest app looks like

This is the smallest useful Nest application, written in TypeScript. It is read-only here because it needs the @nestjs/* packages and decorator support.

TypeScript
// app.controller.ts
import { Controller, Get } from '@nestjs/common';

@Controller('todos')
export class TodosController {
  @Get()
  findAll(): string[] {
    return ['Learn Nest', 'Build an API'];
  }
}
TypeScript
// app.module.ts
import { Module } from '@nestjs/common';
import { TodosController } from './app.controller';

@Module({ controllers: [TodosController] })
export class AppModule {}
TypeScript
// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

With this in place, GET http://localhost:3000/todos returns a JSON array. Notice what is not there: no router object, no call to app.get('/todos', ...), no JSON helper. The @Controller('todos') and @Get() decorators attach metadata (a path prefix and an HTTP method) to the class, and Nest reads that metadata at startup to register the routes for you. Returning an array from the method is enough, because Nest serializes return values to JSON.

The same idea, without decorators

Here is a stand-in that does the same job in plain JavaScript. There are no classes and no decorators: a store, a service and a controller are ordinary objects created by factory functions, and one function at the bottom (the "composition root") wires them together by hand. It runs a real HTTP server on loopback and calls it with fetch.

Node.js A tiny structured app, wired by hand

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Every layer has one job. The controller does not know about HTTP details, the service does not know about the store's internals, and only createServer talks to node:http. That separation is exactly what Nest enforces. The part Nest automates is the last block, the composition root: in a real app you never write createTodoService(store) yourself, you just declare that the service needs a store and the container does the wiring.

Where wiring gets painful

With three objects, manual wiring is fine. Run the next stand-in to see how the number of wiring lines grows as an app gets features. Each feature adds a store, a service and a controller, and the services start depending on each other.

JS Counting the wiring a framework removes

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice the hidden rule: the lines must appear in dependency order, and one forgotten argument is a runtime crash. A dependency injection container solves both problems, and you will build a small one in Providers and Dependency Injection.

Nest or plain Express?

Nest is not always the right tool. Use this as a rough guide.

  • Choose plain Express (or Fastify, Hono) for small services, quick prototypes, serverless functions and teams that want the least abstraction. See Express and Minimal Frameworks for how far a thin framework can go.
  • Choose Nest for medium and large APIs, teams of several developers, long-lived products, and systems that mix HTTP with queues, WebSockets or microservices. Its consistency and testability pay off as the code grows.

There are costs. Nest has a learning curve (decorators, modules, DI), more files and a slightly slower startup than a bare Express app. The runtime overhead per request is small, because Nest resolves most metadata once at startup. Remember too that Nest is not a different server: if you choose Express as the platform, every Express middleware still works.

Nest apps are written in TypeScript by default, and the decorators rely on TypeScript's type metadata. You can use plain JavaScript, but you lose much of the convenience. The TypeScript basics, including decorators, are covered in the TypeScript tutorial.

Nest, Express and Fastify side by side

Nest is not a rival to Express or Fastify; it sits on top of one of them. That makes the comparison less "which is better" and more "what do I get for the extra layer, and what does it cost?"

Topic Plain Express Plain Fastify NestJS (on either)
Project structure Whatever you invent Whatever you invent, plus plugins Modules, controllers and providers by convention
Dependency injection Manual (import and pass objects) Manual, or decorators on the instance Built-in container with scopes
Validation Add Joi, Zod or similar yourself JSON Schema built in Pipes with class-validator or Zod
API documentation Add swagger libraries by hand @fastify/swagger from schemas @nestjs/swagger from decorators
Testing You decide how to build the app for tests app.inject() is excellent Test.createTestingModule with overridable providers
Cross-cutting concerns Middleware only Hooks and plugins Middleware, guards, pipes, interceptors, filters
Raw speed Fine for most services Faster in micro-benchmarks Close to the platform it runs on (Express is the default)
Learning curve Low Low to medium Higher: decorators, modules, DI
TypeScript Optional, add it yourself Good types, optional First-class, assumed

Read the "raw speed" row with care. Fastify handles more requests per second than Express in synthetic tests, and Nest can run on Fastify by changing the adapter (FastifyAdapter). In real services the time goes into your database, network calls and business logic, not the router, so a framework's benchmark rarely decides the outcome. Choose on structure, team size and ecosystem first, and measure your own bottleneck before switching adapters.

A migration path from Express

You do not need a rewrite to move an Express app to Nest, because Nest on the Express platform runs Express middleware as-is. A gradual path:

  1. Create a Nest project and mount your existing Express app (or its routers) inside it, so everything keeps working.
  2. Move one feature at a time into a Nest module: copy its routes into a controller, its logic into a provider, and delete the old router.
  3. Replace hand-written validation with DTOs and a ValidationPipe, and hand-written auth checks with guards.
  4. Move shared code (logging, error formatting) into middleware, interceptors and filters.
  5. When the last router is gone, decide whether to switch the platform to Fastify, and run your load tests before and after.

Each step ships on its own, so old and new code can live side by side for months. See Project Structure and the CLI for where each file goes and Middleware for reusing Express middleware.

What you will build in this tutorial

You will start with controllers, providers and modules, then add the request pipeline (middleware, pipes, guards, interceptors and filters), dependency injection in depth, databases, authentication, documentation, microservices, WebSockets and testing. Four projects tie it together. Every lesson has stand-ins you can run, so by the end you will understand not only how to use each Nest feature but also how it works inside.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is NestJS in relation to Express?

    Nest uses Express (by default) or Fastify as its HTTP platform and adds modules, controllers, providers and a request pipeline on top.
  2. 2. What does a decorator such as @Get() do in Nest?

    Decorators only attach metadata (here, an HTTP method and path). Nest reads that metadata while booting and wires up the routes.
  3. 3. In the hand-wired stand-in, what plays the role of the dependency injection container?

    The stand-in wires objects manually in one place. In Nest the container does that work from the dependencies you declare.
  4. 4. Which project is the best fit for Nest rather than plain Express?

    Nest's conventions pay off as code size and team size grow. For tiny services the extra structure is overhead.

Summary

  • Nest is a TypeScript-first Node.js framework that adds structure on top of Express (default) or Fastify.
  • Its main building blocks are modules, controllers, providers, dependency injection and a request pipeline.
  • Decorators attach metadata, and Nest reads it at startup to register routes and wire classes.
  • The stand-in apps in this tutorial use plain JavaScript to show the mechanism; the real Nest code is read-only here.
  • Choose Nest for larger, long-lived, team-built back ends; choose plain Express or similar for small or very simple services.
  • Nest is not only for HTTP: it also supports microservices, WebSockets and more.

Next: How a Nest App Boots