nest 记录(二)

nest 记录(二)

module controller service

这三层是基本的,没有什么要记录的,多写就知道了。

swragger

1
yarn add @nestjs/swagger @nestjs/platform-express
1
2
3
4
5
// main.tsimport { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';async function bootstrap() {
const app = await NestFactory.create(AppModule); app.setGlobalPrefix('api'); // start 构建文档 const options = new DocumentBuilder()
.setTitle('api文档')
.setVersion('1.0')
.build(); const document = SwaggerModule.createDocument(app, options); SwaggerModule.setup('doc', app, document); // 构建文档结束, 会自动读取代码 await app.listen(3008);}
1
2
// 可以在接口链接后面显示概述 @ApiOperation({
summary: '绑定用户', })
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// DTO的使用import { ApiProperty } from '@nestjs/swagger';import { IsNotEmpty, MinLength, MaxLength } from 'class-validator';export class AdminArticleCreateDto {
@ApiProperty({
description: '标题', default: '标题', })
@MinLength(5, {
message: '标题字数不能小于5个字' })
title: string; @ApiProperty({
description: '内容', default: '内容', })
@IsNotEmpty({
message: '内容不能为空', })
content: string; @ApiProperty({
description: '标签', required: false, default: 1, })
tagId: number; @ApiProperty({
description: '分类ID', required: false, default: 1, })
categoryId: number;}
import { AdminArticlePageDto, AdminArticleCreateDto } from './dto';@ApiOperation({
summary: '提交文章', })
create(@Body() body: AdminArticleCreateDto) {
return this.articleService.create(body); }