setGlobalPrefix 排除端点 Nestjs

Kau*_*dey 3 javascript node.js nestjs

在我的项目中,所有 API 都以/payments. 现在我希望我的一些 API 不带有此前缀。

在我main.ts设置的全局前缀中我使用过。

app.setGlobalPrefix('payments');
Run Code Online (Sandbox Code Playgroud)

一些类似于中间件的解决方案会更干净。

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
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

您可以将第二个参数传递给 setGlobalPrefix 方法,该方法是一个对象,它将包含属性排除,并且该类型应该是一个数组。在该数组中定义路由名称。并且路由将从前缀中排除

app.setGlobalPrefix('v1', { exclude: ['healthcheck'] });
Run Code Online (Sandbox Code Playgroud)

要了解有关 setGlobalPrefix 方法的更多信息,请查看以下链接

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