跳到主要内容

NestJS

  • nestjs/nest
    • 设计来自 Angular + SpringFramework
    • http-errors
  • 核心概念
    • Module
      • @Module()
    • Controller
      • request -> response
      • @Controller('user/me')
      • @Get(),@Post - @HttpCode(204), @Header('Cache-Control', 'none'), @Redirect('https://nestjs.com', 301)
      • @Request(), @Req() req: Request
      • @Response(), @Res() res: Response
      • @Param(key?: string) - req.params
      • @Body(key?: string) - req.body
      • @Query(key?: string) - req.query
      • @Headers(name?: string) - req.headers
      • @Next() next
      • @Ip() - req.ip
      • @HostParam() - req.hosts
      • @Session()
    • Provider
      • @Injectable()
      • @Optional(), @Inject(key?:string)
    • Middleware
    • Exception filter
    • Pipe
    • Guard
    • Interceptor
    • decorator
  • 特性
    • 底层平台无关 - 默认 Express,可用 Fastify
    • 支持 GraphQL、WebSocket
    • combine your config and your code seamlessly by making use of TypeScript decorators
  • 集成
📂 src
├─ 📄 app.controller.spec.ts
├─ 📄 app.controller.ts
├─ 📄 app.module.ts
├─ 📄 app.service.ts
└─ 📄 main.ts

GraphQL

# Express + Apollo 默认
npm i @nestjs/graphql @nestjs/apollo @apollo/server graphql

# Fastify + Apollo
npm i @nestjs/graphql @nestjs/apollo @apollo/server @as-integrations/fastify graphql

# Fastify + Mercurius
npm i @nestjs/graphql @nestjs/mercurius graphql mercurius

Standalone

作为 IoC 容器

  • @nestjs/core
    • 172.1kB, 43.9kB
  • @nestjs/common
    • 86.9kB, 19.4kB
import { NestFactory } from '@nestjs/core';

const app = await NestFactory.createApplicationContext(AppModule);
const userService = app.get(UserService);

await app.close();

FAQ

Reflection metadata 'design:paramtypes' returning undefined

  • 需要 emitDecoratorMetadata
  • esbuild 不支持 emitDecoratorMetadata,swc 支持
# 推荐 swc + tsx
pnpm swc --watch ./src -d ./dist/out
pnpm tsx watch ./dist/out/main.js

# 或 ts-node
ts-node --esm --swc