admin管理员组

文章数量:1410730

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 bee

.139/querybuilder/helloworld 

Swagger page at:

.139/querybuilder/swagger/

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

Request URL: .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();

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 bee

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();
Share Improve this question edited Sep 20, 2020 at 12:02 Sai Gummaluri 1,40410 silver badges17 bronze badges asked Sep 18, 2020 at 10:31 SamuraiJackSamuraiJack 5,56918 gold badges103 silver badges212 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

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')

本文标签: javascriptNestJs swagger missing base pathStack Overflow