Sus*_*tha 8 swagger swagger-ui nestjs
我正在将 swagger-ui 添加到我的 Nestjs 应用程序中。我需要在生产中禁用这种招摇。我搜索了 Nestjs 文档,没有发现任何有用的信息。我需要一个好的资源或指导来禁止生产中的大摇大摆。
Vis*_*amy 11
解决这个问题的方法之一如下。将与 Swagger 文档相关的代码包装在 if 块“ process.env.NODE_ENV !== '生产' ”内。
网址:https://nodejs.dev/learn/nodejs-the-difference- Between -development-and-product
.evn 文件
MONGO_URI="mongodb://localhost:27017/quotesdb"
NODE_ENV=production
Run Code Online (Sandbox Code Playgroud)
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();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8208 次 |
最近记录: |