Backend

NestJS

A progressive TypeScript Node.js framework with modules, decorators and dependency injection.

What it is

NestJS gives the structure Express lacks: modules, controllers, providers and a DI container. Its architecture resembles Angular and Spring.

Express or Fastify runs underneath, your choice. Out of the box it supports validation through pipes, guards for authorization, interceptors and microservice transports.

It is a good fit for large teams: uniform code structure and easy testing thanks to DI. The price is more boilerplate and a steeper learning curve.

Use it when

  • A large backend where the team needs a single architecture.
  • You know Angular or Spring and want familiar patterns.
  • You need GraphQL, WebSocket, queues and microservices in one framework.

Avoid it when

  • A tiny service or prototype: the ceremony outweighs the benefit.
  • The team does not work with TypeScript and decorators.

Key concepts

Module
A unit of code organization grouping controllers and providers.
Provider / DI
A class injected into another class's constructor.
Guard
Checks whether a request may proceed (authorization).
Pipe
Transforms and validates input data.
Interceptor
Wraps a handler call: logging, cache, response mapping.

Example

A controller with an injected service.

Cheat sheet

NestJS cheat sheet
Command or codeWhat it does
npm i -g @nestjs/cliInstalls the Nest CLI
nest new appCreates a project
nest g resource usersGenerates module, controller, service
@Module({ controllers, providers })Module declaration
@Controller('users')Controller with a route prefix
@Get(':id') find(@Param('id') id: string)GET handler with a parameter
@Post() create(@Body() dto: CreateDto)POST handler with a body
@Injectable()Marks a service for DI
@UseGuards(AuthGuard)Protects a route
app.useGlobalPipes(new ValidationPipe())Global DTO validation
npm run test:e2eRuns e2e tests

How to learn it

  1. Learn TypeScript and Node.js basics with Express.
  2. Understand decorators and dependency injection on simple examples.
  3. Create a project with the Nest CLI and study the module structure.
  4. Write controllers, services and providers.
  5. Add validation with DTOs, ValidationPipe and class-validator.
  6. Connect a database with Prisma or TypeORM.
  7. Implement authentication with guards and JWT.
  8. Write the unit and e2e tests that Nest generates.

Interview questions

  1. 1. What is the DI container for?

    It creates dependencies and passes them into classes, so code stays loosely coupled. In tests you can swap a service for a mock easily.

  2. 2. How does a guard differ from middleware?

    A guard knows the execution context (which controller and method) and decides whether to let the request in. Middleware runs earlier and knows nothing about the handler.

  3. 3. Express or Fastify underneath?

    Nest supports both through adapters. Fastify is faster, Express has wider middleware compatibility.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).