1、安装
下载swagger
npm install --save @nestjs/swagger swagger-ui-express
按照官网的介绍进行配置
main.ts文件
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
// 标题
.setTitle('NestJs博客API')
// 描述
.setDescription('我的第一个NestJs项目')
// 版本
.setVersion('1.0')
//标签
.addTag('cats')
.build();
const document = SwaggerModule.createDocument(app, config);
// 接口文档路径
SwaggerModule.setup('api-docs', app, document);
// 监听端口号3000
await app.listen(3000);
}
bootstrap();
2、添加标签
import { Controller, Get, Post } from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
@Controller('posts')
//添加标签
@ApiTags('帖子')
export class PostsController {
@Get()
//添加描述
@ApiOperation({summary:'显示博客列表'})
index() {
return [
{id:1111},
{id:13},
{id:14},
{id:1},
{id:37},
{id:26},
];
}
@Post()
create(){
return{
success:true
}
}
@Get(':id')
detail(){
return{
id:1,
title:'aaaaa'
}
}
}