how to disable swagger for production in nestjs

Viewed 3414

I am adding the swagger-ui to my nestjs app. I need to disable that swagger in production. I searched over the nestjs documentation, I didn't find any useful. I need a good resource or guidance to disable the swagger in production.

1 Answers

One of the ways to solve this is as below. Wrap the Swagger Documentation-related code within if block "process.env.NODE_ENV !== 'production'".

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

.evn file

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

main.ts file

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

Related