Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 229
SamuraiJack Avatar asked Oct 30 '25 09:10

SamuraiJack


1 Answers

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 (e.g. 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')
like image 97
sashko Avatar answered Nov 02 '25 00:11

sashko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!