web framework
NestJS is a framework for building server-side Node.js applications with a strong emphasis on architecture, modularity, and TypeScript. It uses decorators and dependency injection to enforce a structured approach similar to Angular, making it particularly well-suited for teams building complex, maintainable applications. Under the hood, it abstracts Express by default (or Fastify as an alternative) while exposing their full APIs for flexibility.
The framework emerged to address the lack of opinionated structure in the Node.js ecosystem. While Express gives you complete freedom, that freedom often leads to inconsistent codebases as projects scale. NestJS provides guardrails through its module system, dependency injection container, and decorator-based routing, without sacrificing access to lower-level HTTP platform features when needed.
Developers coming from strongly-typed backend frameworks (Spring, .NET, Django) or Angular frontends find NestJS familiar. It's widely adopted by enterprise teams, startups building microservices, and projects requiring built-in support for GraphQL, WebSockets, or distributed systems. The CLI scaffolds projects with testing utilities, configuration management, and clear separation of concerns from day one.
NestJS isn't just TypeScript wrappers around Express—it includes a comprehensive ecosystem for guards (authentication), interceptors (request/response transformation), pipes (validation), exception filters, and microservice communication patterns (Redis, MQTT, gRPC). This makes it a complete platform for building production APIs rather than just a routing library.
import { Controller, Get, Post, Body, Injectable, Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
// Service with business logic
@Injectable()
class UsersService {
private users = [{ id: 1, name: 'Alice' }];
findAll() {
return this.users;
}
create(name: string) {
const user = { id: this.users.length + 1, name };
this.users.push(user);
return user;
}
}
// Controller with dependency injection
@Controller('users')
class UsersController {
constructor(private usersService: UsersService) {}
@Get()
getUsers() {
return this.usersService.findAll();
}
@Post()
createUser(@Body('name') name: string) {
return this.usersService.create(name);
}
}
// Module wiring dependencies
@Module({
controllers: [UsersController],
providers: [UsersService],
})
class AppModule {}
// Bootstrap application
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log('Server running on http://localhost:3000');
}
bootstrap();Enterprise REST APIs: Large teams building multi-module applications benefit from enforced structure, dependency injection for testing, and built-in OpenAPI/Swagger documentation generation. Controllers handle routing while services contain business logic, making codebases predictable across dozens of developers.
Microservices architectures: NestJS includes native transport layers for TCP, Redis, NATS, RabbitMQ, and Kafka. You can build polyglot systems where services communicate via message patterns, with decorators like @MessagePattern() handling routing instead of HTTP endpoints.
GraphQL APIs: The @nestjs/graphql module integrates Apollo Server with code-first or schema-first approaches. Resolvers are just decorated classes, and the DI system handles injecting database services, making GraphQL feel like standard NestJS controllers.
Real-time applications: WebSocket support through @nestjs/websockets works with Socket.io or native WS. Gateways use decorators like @SubscribeMessage() while sharing the same DI container as HTTP controllers, enabling code reuse between REST and real-time endpoints.
Monorepo API backends: Teams consolidating multiple services into a monorepo use NestJS modules as boundaries. Shared modules (auth, logging, database) get imported across apps, and the CLI supports workspace mode for managing multiple entry points with shared code.
npm install nestjspnpm add nestjsbun add nestjs