Skip to content
Home/ Backend/ Nest/ Basic
作者:  WHY
字数统计: 
阅读时长:  分钟
阅读量:  1091
本站总访问量:  1091 本站访客数:  937

Nest Basic

Reference

说明

  • 推荐使用 class 作为 DTO, 如 class UserDto
  • 如果使用 TypeORMPrisma, 则使用 *.entity.ts 文件定义
  • 使用 class-validatorclass-transformerDTO 进行校验
  • 使用 @nestjs/schedule 实现定时任务
  • 使用 axios@nestjs/axios 发起 HTTP 请求
  • 使用 @nestjs/throttler 进行限流 (Rate Limiting), JAVA 可使用高性能限流器Guava RateLimiter
  • 使用 helmet@fastify/helmet 配置更安全的 Headers
  • nestjs 提供了 devtools, 可以可视化展示和调试
  • 使用 @nestjs/swagger 展示 API 文档和在线调试 (OpenAPI)

文件后缀说明:

  • *.module.ts 模块
  • *.controller.ts 控制器
  • *.service.ts 服务
  • *.provider.ts 提供者
  • *.middleware.ts 中间件
  • *.decorator.ts 装饰器
  • *.guard.ts 守卫
  • *.pipe.ts 管道
  • *.interceptor.ts 拦截器
  • *.filter.ts 异常过滤 (处理) 器
  • *.dto.ts DTO, Data Transfer Object 数据传输对象
  • *.entity.ts Entity 实体
  • *.interface.ts 接口
  • *.spec.ts 测试文件

接收参数

@Param

@Query

@Body

@Headers

ts

作用域

  • method scoped controller/resolver/gateway
  • controller scoped
  • global scoped

控制器

controller

方法

全局

Use Decorators

Use 系列装饰器:

  • UseGuardsUseGlobalGuards
  • UsePipesUseGlobalPipes
  • UseInterceptorsuseGlobalInterceptors
  • UseFiltersUseGlobalFilters

UseGuards

全局:

ts
// main.ts
app.useGlobalGuards(new RoleGuard(new Reflector()));
// app.useGlobalGuards(new RoleGuard(app.get(Reflector)));
ts
// app.module.ts
@Module({
  providers: [
    {
      provide: APP_GUARD,
      useClass: RoleGuard,
    },
  ],
})

UsePipes

全局:

ts
// main.ts
app.useGlobalPipes(new CustomValidationPipe());
ts
// app.module.ts
@Module({
  providers: [
    {
      provide: APP_PIPE,
      useClass: CustomValidationPipe,
    },
  ],
})

UseInterceptors

全局:

ts
// main.ts
app.useGlobalInterceptors(new LoggerInterceptor());
ts
// app.module.ts
@Module({
  providers: [
    {
      provide: APP_INTERCEPTOR,
      useClass: LoggerInterceptor,
    },
  ],
})

UseFilters

全局:

ts
// main.ts
app.UseGlobalFilters(new HttpExceptionFilter());
ts
// app.module.ts
@Module({
  providers: [
    {
      provide: APP_FILTER,
      useClass: HttpExceptionFilter,
    },
  ],
})

controller 中设置响应头:

ts
@Get()
@Header('Content-Type', 'text/plain')
@Header('Content-Disposition', 'attachment; filename="README.md"')

或者在路由内:

ts
// express 写法
// TODO fastify 写法
@Get()
download(@Res({ passthrough: true }) res: Response) {
	res.set({
		'Content-Type': 'text/plain',
		'Content-Disposition': 'attachment; filename="README.md"',
	})
}

在服务内:

ts
// download.service.ts
import { StreamableFile } from '@nestjs/common';
return new StreamableFile(Buffer.from('# README'), {
	type: 'text/plain',
	disposition: 'attachment; filename=${README.md}',
}

Redis

mac 安装 Redis:

bash
brew install redis
bash
# 1
brew service start redis
# 2
# http://download.redis.io/redis-stable/redis.conf
redis-server /usr/local/etc/redis.conf
bash
mkdir /docker-data/redis

# docker run --name redis -p 6379:6379 -d redis --restart=always --requirepass "password"
docker run --name redis \
-p 6379:6379 \
-v /docker-data/redis/redis.conf:/etc/redis/redis.conf \
-v /docker-data/redis:/data \
-d redis redis-server /etc/redis/redis.conf --appendonly yes
  • -p 6379 6379 端口映射
  • -v 前者为宿主机, 后者为 docker 容器
  • -d redis redis-server /etc/redis/redis.conf 以容器内 /etc/redis/redis.conf 配置文件在后台启动 docker
  • --appendonly yes 开启 redis 持久化
  • --restart 设置重启策略
  • --requirepass 设置密码

class-validator

Contributors

The avatar of contributor named as why why

Changelog

Released under the MIT License.

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.