Configs2026年4月11日·1 分钟阅读

NestJS — Progressive Node.js Framework for Enterprise Apps

NestJS is a progressive Node.js framework for building efficient, scalable, and enterprise-grade server-side applications with TypeScript. It uses modular architecture inspired by Angular and supports Express/Fastify underneath.

AI
AI Open Source · Community
快速使用

先拿来用,再决定要不要深挖

这里应该同时让用户和 Agent 知道第一步该复制什么、安装什么、落到哪里。

npm i -g @nestjs/cli
nest new project-name
cd project-name
npm run start:dev

Generate a resource:

nest g resource users
介绍

NestJS is a framework for building efficient, scalable Node.js server-side applications. Built with TypeScript (fully supports vanilla JavaScript) and combines elements of OOP, FP, and FRP. Under the hood it uses Express by default but can swap to Fastify.

What NestJS Does

Provides out-of-the-box architecture for testable, scalable, loosely-coupled, and maintainable backend apps:

  • Modules — organize code by feature
  • Controllers — handle incoming HTTP requests
  • Providers/Services — business logic via dependency injection
  • Pipes/Guards/Interceptors — cross-cutting concerns
  • Microservices — Kafka, RabbitMQ, gRPC, Redis, NATS transport
  • GraphQL — code-first or schema-first
  • WebSockets — real-time gateways

Architecture

Angular-inspired modular architecture: every app has a root AppModule that imports feature modules. Each feature module declares controllers and providers. DI container wires everything at startup.

AppModule
├── UsersModule (UsersController, UsersService)
├── AuthModule (AuthController, AuthService, JwtStrategy)
└── DatabaseModule (TypeOrmModule.forRoot)

Self-Hosting

# Production build
npm run build
node dist/main.js

# Docker
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY dist ./dist
CMD ["node", "dist/main.js"]

Key Features

  • TypeScript-first with decorators
  • Built-in DI container
  • Express or Fastify HTTP adapter
  • OpenAPI/Swagger auto-generation
  • TypeORM/Prisma/Mongoose integrations
  • CLI for scaffolding
  • Testing utilities (Jest-based)

Comparison

Framework DI TypeScript Opinionated Best For
NestJS Built-in First-class Yes Enterprise backends
Express Manual Optional No Minimal APIs
Fastify Manual Good No High-perf APIs
Koa Manual Optional No Middleware composition

常见问题 FAQ

Q: NestJS 和 Express 的关系? A: NestJS 默认使用 Express 作为 HTTP 层,但抽象了路由/中间件,提供更高层的模块/DI 架构。可以替换为 Fastify。

Q: 性能怎么样? A: 使用 Fastify adapter 后性能接近原生 Fastify。默认 Express adapter 的开销来自抽象层。

Q: 学习曲线陡吗? A: 如果熟悉 Angular 几乎零门槛;Express 用户需要理解装饰器和 DI。

来源与致谢 Sources

讨论

登录后参与讨论。
还没有评论,来写第一条吧。

相关资产