Nodejs Nestjs API文档插件@nestjs/swagger-ui的使用
Nodejs Nestjs API文档插件@nestjs/swagger-ui的使用[@nestjs](/user/nestjs)/swagger
是一个用于 NestJS 项目中生成 Swagger 文档的强大工具。它可以帮助你自动生成 API 文档,并且通过 Swagger UI 可视化这些文档。这里是如何使用 [@nestjs](/user/nestjs)/swagger
和 swagger-ui-express
来为你的 NestJS 应用程序创建 API 文档。
步骤1:安装必要的依赖
首先,你需要在你的 NestJS 项目中安装 [@nestjs](/user/nestjs)/swagger
和 swagger-ui-express
:
npm install [@nestjs](/user/nestjs)/swagger swagger-ui-express
步骤2:配置 Swagger
接下来,在你的应用程序中配置 Swagger。这通常在主模块(通常是 app.module.ts
)中完成。
import { Module } from '[@nestjs](/user/nestjs)/common';
import { NestFactory } from '[@nestjs](/user/nestjs)/core';
import { DocumentBuilder, SwaggerModule } from '[@nestjs](/user/nestjs)/swagger';
import { AppModule as YourAppModule } from './app.module'; // 你的主模块
import * as express from 'express';
@Module({
imports: [YourAppModule],
})
export class AppModule {
static forRoot() {
return AppModule;
}
}
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// 配置Swagger
const config = new DocumentBuilder()
.setTitle('API Title')
.setDescription('The API description')
.setVersion('1.0')
.addTag('your-tag') // 可以添加多个标签
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
// 如果你想同时使用 swagger-ui-express
app.use('/swagger', express.static('node_modules/swagger-ui-express'));
await app.listen(3000);
}
bootstrap();
步骤3:标记控制器和方法
为了生成正确的文档,你需要在控制器和方法上使用适当的装饰器。例如:
import { Controller, Get, Param, Query, Post, Body } from '[@nestjs](/user/nestjs)/common';
import { ApiOperation, ApiResponse, ApiTags } from '[@nestjs](/user/nestjs)/swagger';
@ApiTags('example') // 这个标签需要与你在Swagger配置中的标签相匹配
@Controller('example')
export class ExampleController {
@Get(':id')
@ApiOperation({ summary: '获取示例' })
@ApiResponse({ status: 200, description: '返回示例数据' })
getExample(@Param('id') id: string) {
return { id };
}
@Post()
@ApiOperation({ summary: '创建示例' })
@ApiResponse({ status: 201, description: '创建成功' })
createExample(@Body() body: any) {
return body;
}
}
步骤4:运行应用
启动你的 NestJS 应用后,你应该能在 http://localhost:3000/api
看到 Swagger UI 自动加载了你的 API 文档。
以上就是如何在 NestJS 中使用 [@nestjs](/user/nestjs)/swagger
创建 API 文档的基本步骤。你可以根据需要进一步定制和扩展 Swagger 的功能。
当然,要为你的NestJS项目添加Swagger UI文档,你可以使用@nestjs/swagger
和@nestjs/swagger-ui
。首先,安装必要的包:
npm install @nestjs/swagger @nestjs/swagger-ui swagger-ui-express
然后,在你的AppModule中配置Swagger:
import { Module } from '@nestjs/common';
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
@Module({
imports: [],
})
export class AppModule {
static async forRoot() {
const app = await NestFactory.create(AppModule);
const config = new DocumentBuilder()
.setTitle('你的项目名称')
.setDescription('项目的描述')
.setVersion('1.0')
.addTag('标签')
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
}
现在,当你启动应用时,访问/api
就能看到漂亮的Swagger UI界面啦!是不是很酷?
@nestjs/swagger
是一个用于 NestJS 的库,可以帮助您自动生成 API 文档。它利用了 Swagger 的功能来生成交互式的 API 文档。下面是如何在您的 NestJS 项目中使用 @nestjs/swagger
来创建 API 文档。
步骤 1: 安装依赖
首先,您需要安装 @nestjs/swagger
和 swagger-ui-express
:
npm install @nestjs/swagger swagger-ui-express
步骤 2: 配置 Swagger
接下来,您需要配置 Swagger 在您的应用中生效。通常,这可以在主模块(通常是 AppModule
)或任何其他模块中完成。
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// 设置 Swagger 文档的基本配置
const config = new DocumentBuilder()
.setTitle('API 文档')
.setDescription('API 描述')
.setVersion('1.0')
.addTag('users') // 可以添加多个标签
.build();
// 创建 Swagger 文档
const document = SwaggerModule.createDocument(app, config);
// 将 Swagger 文档暴露为路由
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();
步骤 3: 使用装饰器定义 API 文档
在控制器中,您可以使用各种装饰器来定义 API 的结构和行为。例如:
import { Controller, Get, Param, Query, Post, Body, Patch, Delete, Version } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiQuery } from '@nestjs/swagger';
@Controller('users')
@ApiTags('users') // 标签
export class UsersController {
@Get()
@ApiOperation({ summary: '获取用户列表' })
@ApiQuery({ name: 'page', type: Number, required: false }) // 查询参数
@ApiResponse({
status: 200,
description: '返回用户列表',
type: [User], // 返回类型
})
async findAll(@Query('page') page: number) {
return [];
}
@Post()
@ApiOperation({ summary: '创建用户' })
@ApiResponse({
status: 201,
description: '用户创建成功',
type: User, // 返回类型
})
@ApiResponse({
status: 400,
description: '无效的请求参数',
})
async create(@Body() user: User) {
return {};
}
}
总结
通过上述步骤,您可以轻松地在 NestJS 项目中集成和使用 Swagger 自动生成 API 文档。Swagger 提供了一个强大的界面来展示和测试您的 API 端点,使开发更加高效和透明。
[@nestjs](/user/nestjs)/swagger
是用于 NestJS 的 API 文档生成工具,而 swagger-ui
提供了一个可视化界面。使用步骤如下:
-
安装依赖:
npm install [@nestjs](/user/nestjs)/swagger swagger-ui-express
-
配置模块:
import { NestFactory } from '[@nestjs](/user/nestjs)/core'; import { DocumentBuilder, SwaggerModule } from '[@nestjs](/user/nestjs)/swagger'; import { AppModule } from './app.module'; async function bootstrap() { const app = await NestFactory.create(AppModule); const config = new DocumentBuilder() .setTitle('API Title') .setDescription('API Description') .setVersion('1.0') .addTag('tags') .build(); const document = SwaggerModule.createDocument(app, config); SwaggerModule.setup('api', app, document); await app.listen(3000); } bootstrap();
-
在控制器中使用
@ApiTags
,@ApiOperation
, 等装饰器来添加文档注解。