【问题标题】:how to disable swagger for production in nestjs如何在nestjs中禁用swagger进行生产
【发布时间】:2021-08-09 09:00:23
【问题描述】:

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

【问题讨论】:

    标签: swagger nestjs swagger-ui


    【解决方案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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-07
      • 2018-08-04
      • 1970-01-01
      • 2018-11-29
      • 2016-02-20
      • 2021-09-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多