【问题标题】:Nestjs: middleware for all routes except /authNestjs:除 /auth 之外的所有路由的中间件
【发布时间】:2020-07-23 22:49:40
【问题描述】:

我最近开始使用 nestjs,我想为所有路由应用一个中间件,除了 auth 路由。

在文档中说我可以将所有控制器或路由的路径添加到我想要中间件的位置,但我发现这种方式相当昂贵

.forRoutes(
     SearchController,
     Other,
     Other,
     Other
);

所以我想知道是否有解决方案,或者您是否设法将正则表达式用于这样的事情:

.forRoutes(
    {path: 'All route except /auth', method: RequestMethod.ALL}
);

【问题讨论】:

  • 你解决过这个问题吗?我似乎无法让 exclude 在 Azure 函数中工作。

标签: regex typescript routes middleware nestjs


【解决方案1】:

现在我们使用函数exclude

export class YourModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(YourMiddleware)
      .exclude('auth')
      .forRoutes('*');
  }
}

更多信息:https://docs.nestjs.com/middleware#excluding-routes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 2013-10-19
    • 2020-12-29
    • 2017-08-25
    • 1970-01-01
    • 2019-06-09
    相关资源
    最近更新 更多