多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
## 其他特性 ### 全局前缀 要忽略一个通过`setGlobalPrefix()`配置的全局前缀, 使用`ignoreGlobalPrefix`: ```TypeScript const document = SwaggerModule.createDocument(app, options, { ignoreGlobalPrefix: true, }); ``` ### 多重声明 `Swagger`模块提供了一个支持多重声明的方法,也就是说可以在多个终端提供多个界面和多个文档。 要支持多重声明,首先在模块中要进行声明,在`createDocument()`方法中传递第3个参数,`extraOptions`,这是个包含一个叫做`include`名称的属性,该属性提供了一个由模块组成的数组。 可以如下配置多重声明: ```TypeScript 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); /** * createDocument(application, configurationOptions, extraOptions); * * createDocument method takes an optional 3rd argument "extraOptions" * which is an object with "include" property where you can pass an Array * of Modules that you want to include in that Swagger Specification * E.g: CatsModule and DogsModule will have two separate Swagger Specifications which * will be exposed on two different SwaggerUI with two different endpoints. */ const options = new DocumentBuilder() .setTitle('Cats example') .setDescription('The cats API description') .setVersion('1.0') .addTag('cats') .build(); const catDocument = SwaggerModule.createDocument(app, options, { include: [CatsModule], }); SwaggerModule.setup('api/cats', app, catDocument); const secondOptions = new DocumentBuilder() .setTitle('Dogs example') .setDescription('The dogs API description') .setVersion('1.0') .addTag('dogs') .build(); const dogDocument = SwaggerModule.createDocument(app, secondOptions, { include: [DogsModule], }); SwaggerModule.setup('api/dogs', app, dogDocument); await app.listen(3000); } bootstrap(); ``` 现在可以使用以下命令启动服务器: ```bash $ npm run start ``` 访问`http://localhost:3000/api/cats`以查看`cats`的 Swagger UI: ![](https://docs.nestjs.com/assets/swagger-cats.png) 反过来,`http://localhost:3000/api/dogs`将为`dogs`公开 Swagger UI: ![](https://docs.nestjs.com/assets/swagger-dogs.png)