如何在 nestjs 中为生产禁用 swagger
how to disable swagger for production in nestjs
我正在将 swagger-ui 添加到我的 nestjs 应用程序中。我需要在生产中禁用这种招摇。我搜索了 nestjs 文档,没有找到任何有用的东西。我需要一个好的资源或 guidance 来禁用生产中的招摇。
解决此问题的方法之一如下。将与 Swagger 文档相关的代码包装在 if 块“process.env.NODE_ENV !== 'production'”.
中
URL: https://nodejs.dev/learn/nodejs-the-difference-between-development-and-production
.evn 文件
MONGO_URI="mongodb://localhost:27017/quotesdb"
NODE_ENV=production
main.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();
我正在将 swagger-ui 添加到我的 nestjs 应用程序中。我需要在生产中禁用这种招摇。我搜索了 nestjs 文档,没有找到任何有用的东西。我需要一个好的资源或 guidance 来禁用生产中的招摇。
解决此问题的方法之一如下。将与 Swagger 文档相关的代码包装在 if 块“process.env.NODE_ENV !== 'production'”.
中URL: https://nodejs.dev/learn/nodejs-the-difference-between-development-and-production
.evn 文件
MONGO_URI="mongodb://localhost:27017/quotesdb"
NODE_ENV=production
main.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();