我已经从外部文档库生成了swagger.yaml/swagger.json.现在,我想将其作为API文档导入并托管在运行在NestJS上的API平台上.我知道,我们可以从NestJS代码生成和导出swagger,但对我来说,需求是相反的,我们有swagger.json,我们需要在NestJS平台上呈现它,比如https://example.com/docs
我已经从外部文档库生成了swagger.yaml/swagger.json.现在,我想将其作为API文档导入并托管在运行在NestJS上的API平台上.我知道,我们可以从NestJS代码生成和导出swagger,但对我来说,需求是相反的,我们有swagger.json,我们需要在NestJS平台上呈现它,比如https://example.com/docs
如果您已经有一个swagger.json
文件,您可以忽略SwaggerModule.createDocument
和DocumentBuilder
,只将解析后的JSON文件传递给SwaggerModule.setup()
import { NestFactory } from '@nestjs/core';
import { SwaggerModule } from '@nestjs/swagger';
import { readFile } from 'fs/promises';
import { path } from 'join';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// read the JSON file to string and parse the string to object literal
const document = JSON.parse(
(await readFile(join(process.cwd(), 'swagger.json'))).toString('utf-8')
)
SwaggerModule.setup('api', app, document);
await app.listen(3000);
}
bootstrap();