节点expressJwt,除非指定id路由 [英] node expressJwt unless specify id route

查看:239
本文介绍了节点expressJwt,除非指定id路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用expressJwt库,我希望能够排除以下路由api/items/:idGET,但不包括任何看起来像api/items/:id/special-action的路由.

We are using the expressJwt library and I want to be able to exclude the GET for the following route api/items/:id but not include any routes that look like api/items/:id/special-action.

到目前为止,我只能排除所有具有:id的路线.

So far, I've only been able to exclude all routes that have the :id.

以下是我们如何排除具有:idGET条路线.

Below is how we've achieved excluding GET routes that have :id.

this.app.use(expressJwt({ secret: secrets.JWT }).unless({
  path: [
    ...
    { url: /\/api\/items\/(.)/, methods: ['GET'] },
    ...
  ]
});

我尝试过{ url: /\/api\/items\/(.)\//, methods: ['GET'] },但是它与任何路由都不匹配,因此不会排除具有:id的路由.

I've tried { url: /\/api\/items\/(.)\//, methods: ['GET'] } but it then does not match to any routes and so no routes with :id get excluded.

我使用正则表达式的方式有些问题,但是我很难看到它.我是否需要将(.)更改为不完全匹配?我想它可能与斜杠匹配

There's something off in the way I'm using the regex, but I'm having a hard time seeing it. Do I need to change the (.) to not be a match all? I imagine it might be matching the trailing slashes

推荐答案

您可以使用

/^\/api\/items\/([^\/]*)$/

[^\/]*否定的字符类匹配除/之外的0个或更多字符,并且^/$锚确保模式将针对整个字符串进行尝试.

The [^\/]* negated character class matches 0 or more chars other than / and the ^ / $ anchors make sure the pattern will be tried against the whole string.

请参见 regex演示.

详细信息

  • ^-字符串锚点的开始
  • \/api\/items\/-匹配文字/api/items/子字符串
  • ([^\/]*)-捕获组1:/以外的任何0+个字符.
  • $-字符串锚点的结尾. 请注意,如果要确保[^\/]*之后没有/,请在字符串结尾处添加$字符串锚结尾.
  • ^ - start of string anchor
  • \/api\/items\/ - matches a literal /api/items/ substring
  • ([^\/]*) - Capturing group 1: any 0+ chars other than /.
  • $ - end of string anchor. Note that in case you want to make sure the [^\/]* has no / after, add a $ end of string anchor at the end.

这篇关于节点expressJwt,除非指定id路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆