Index error 404 when add swagger to Nestjs

Viewed 472

My app is showing message look like "Server is running" at index url http://localhost:5001. But when i added swagger-ui for testing api document. I only able to access http://localhost:5001/api/docs. My index page return {"statusCode":404,"message":"Cannot GET /","error":"Not Found"}. when i try request by postman it works fine. Is there a way to display the original "server is running" message. Tks for your help!

open-api/index.ts:

import { INestApplication } from '@nestjs/common';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import {
  SWAGGER_API_CURRENT_VERSION,
  SWAGGER_API_DESCRIPTION,
  SWAGGER_API_NAME,
  SWAGGER_API_ROOT,
} from '../core/constants';

export const setupSwagger = (app: INestApplication) => {
  const options = new DocumentBuilder()
    .setTitle(SWAGGER_API_NAME)
    .setDescription(SWAGGER_API_DESCRIPTION)
    .setVersion(SWAGGER_API_CURRENT_VERSION)
    .addBearerAuth()
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup(SWAGGER_API_ROOT, app, document);
};

open-api/index.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ValidateAuthMiddleware } from './core/middlewares/validate-auth.middleware';
import { setupSwagger } from './open-api';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  setupSwagger(app);
  app.useGlobalPipes(new ValidateAuthMiddleware());
  await app.listen(5001);
}
bootstrap();

enter image description here

enter image description here

0 Answers
Related