Wener Notes

NestJS

约 2 分钟阅读
  • nestjs/nestGitHubnestjs/nestnestjs/nest · 设计来自 Angular + SpringFramework · http-errors · 核心概念 · Module · - · Controller · request -> response · - · , - , ,笔记:NestJS
    • 设计来自 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
  • 集成
text
📂 src
├─ 📄 app.controller.spec.ts
├─ 📄 app.controller.ts
├─ 📄 app.module.ts
├─ 📄 app.service.ts
└─ 📄 main.ts

GraphQL

bash
# 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
TypeScript
import { NestFactory } from '@nestjs/core';

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

await app.close();

FAQ

Reflection metadata 'design
' returning undefined

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

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

关联信息

反向链接、本文链接的其他页面和外部资料。

反向链接

  • JavaScript Awesome
    笔记 · nestjs

    novnc/noVNC · felixrieseberg/macintosh.js · RobinCsl/awesome-js-tooling-not-in-js · sorrycc/awesome-javascript · CLI / Table

  • nestia
    笔记 · NestJS

    基于 NestJS · 生成类似 tRPC 的客户端 · 使用 ts-patch 注入自定义 ts 插件实现高级功能 · 使用 typia · tsconfig.json

  • Nodejs Awesome
    笔记 · nestjs

    最好选择 TypeScript 开发的或支持 TypeScript 的 · TypeScript 的 decorator 比 Java 的 Annotation 弱得多 · 不支持获取字段实际类型信息 - 因为不存在这样的信息 · 最近一两年 (2020-2021),可能是因为 Go 和 Rust 的盛行,导致 Nodejs 的后端开发弱化了,很多项目...

References

GitHub5 条
Packages7 条
最近更新commit e2774a9Edit

On this page