简体   繁体   中英

NestJs swagger missing base path

Everything works fine on local machine. The problem arises after deployment. After deployment, /querybuilder gets appended to the base url. So,

http://localhost:80/helloworld 

would become

http://52.xxx.xxx.139/querybuilder/helloworld 

Swagger page at:

http://52.xxx.xxx.139/querybuilder/swagger/

When I execute a method through swagger ui page: This is what I see in networks tab:

Request URL: http://52.xxx.xxx.139/

Controller:

@Controller()
export class AppController {
  constructor(private readonly appService: AppService) {}

  @Get()
  getHello(): string {
    return this.appService.getHello();
  }
}

Main.ts

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  const options = new DocumentBuilder()
  .setTitle('Query Builder - MongoDb Parser')
  .setDescription("This is Query Builder's MongoDb Parser takes database agnostic queries and transpiles it into native MongoDb query.")
  .setVersion('1.0')
  .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);
  await app.listen(80);
}
bootstrap();

In most cases the most appropriate way to achieve this is by using setGlobalPrefix method which adds prefix to your API endpoints and URL used by swagger.

const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('querybuilder');

However, if your routing is handled by external server (eg nginx), you can add the prefix only to the URL used by swagger using addServer method.

const options = new DocumentBuilder()
  .setTitle('Query Builder - MongoDb Parser')
  .addServer('/querybuilder')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM