如何在 nestjs 中禁用用于生产的 swagger

时间:2021-05-20 06:00:25

标签: swagger nestjs swagger-ui

我正在将 swagger-ui 添加到我的 nestjs 应用程序中。我需要在生产中禁用这种招摇。我搜索了 nestjs 文档,没有找到任何有用的信息。我需要一个好的资源或指导来在生产中禁用招摇。

1 个答案:

答案 0 :(得分:1)

解决此问题的方法之一如下。将 Swagger 文档相关的代码包装在 if 块“process.env.NODE_ENV !=='production'”中。

网址:https://nodejs.dev/learn/nodejs-the-difference-between-development-and-production

.evn 文件

MONGO_URI="mongodb://localhost:27017/quotesdb"
NODE_ENV=production

ma​​in.ts 文件

import 'dotenv/config';

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);

  app.enableCors();

  if (process.env.NODE_ENV !== 'production') {
    const options = new DocumentBuilder()
      .setTitle('Quotes Api')
      .setDescription('Quotes API Description')
      .setVersion('1.1')
      .addTag('quotes')
      .build();

    const document = SwaggerModule.createDocument(app, options);
    SwaggerModule.setup('api/swagger', app, document);
  }

  await app.listen(3000);
}
bootstrap();

enter image description here