admin管理员组

文章数量:1287803

In my project all the APIs are prefixed with /payments. Now I want some of my APIs not to be prefixed with this.

In my main.ts to set global prefix I've used.

app.setGlobalPrefix('payments');

Some solution similar to middleware would be cleaner.

export class AppModule implements NestModule {
  constructor(private configService: ConfigService) { }
  public configure(consumer: MiddlewareConsumer) {
    if (this.configService.get('REQUIRE_AUTH') !== 'FALSE') {
      consumer
        .apply(AuthMiddleware)
        .exclude({
          path: 'platform/healthcheck',
          method: RequestMethod.ALL,
        })
        // Like this
    }
  }
}

In my project all the APIs are prefixed with /payments. Now I want some of my APIs not to be prefixed with this.

In my main.ts to set global prefix I've used.

app.setGlobalPrefix('payments');

Some solution similar to middleware would be cleaner.

export class AppModule implements NestModule {
  constructor(private configService: ConfigService) { }
  public configure(consumer: MiddlewareConsumer) {
    if (this.configService.get('REQUIRE_AUTH') !== 'FALSE') {
      consumer
        .apply(AuthMiddleware)
        .exclude({
          path: 'platform/healthcheck',
          method: RequestMethod.ALL,
        })
        // Like this
    }
  }
}
Share Improve this question asked Dec 21, 2022 at 10:05 Kaushik KakdeyKaushik Kakdey 6736 silver badges11 bronze badges 2
  • Instead of a global prefix you would have to set the prefix on the controller and then you can pick and choose what endpoints have it – mh377 Commented Dec 21, 2022 at 13:07
  • Did you read the docs? – Jay McDoniel Commented Dec 21, 2022 at 15:48
Add a ment  | 

1 Answer 1

Reset to default 12

You can pass the second parameter to setGlobalPrefix method which is an object, that would contain the property exclude and that type should be an array. Define the route names in that array. and that routes will be excluded from the prefix

app.setGlobalPrefix('v1', { exclude: ['healthcheck'] });

To see more about the setGlobalPrefix method check the below link

https://docs.nestjs./faq/global-prefix

本文标签: javascriptsetGlobalPrefix exclude endpoint nestjsStack Overflow