Handling parameterised routes in express-jwt using unless

橙三吉。 提交于 2020-01-01 08:59:11

问题


Given the following route:

router.get('/api/members/confirm/:id, function (req, res, next)

how do I specify the route to be excluded? I have tried:

app.use('/api', expressJwt({ secret: config.secret}).unless({path: ['/api/members/confirm']}));

and

app.use('/api', expressJwt({ secret: config.secret}).unless({path: ['/api/members/confirm/:id']}));

but neither path in the unless array seem to work?


回答1:


The express-jwt module is using express-unless to give you this unless method, which doesn't accept express' :param path arguments syntax.

But it does accept a regex, so you could do this:

app.use('/api', expressJwt({ secret: config.secret}).unless({path: [/^\/api\/members\/confirm\/.*/]}));

If you don't like that, you can also give unless a function:

var myFilter = function(req) {return true;}
app.use('/api', expressJwt({ secret: config.secret}).unless(myFilter));



回答2:


You must be using regex to achieve what you are looking for, and you can also add in another field to specify what methods are allowed like so:

expressJwt({ secret, isRevoked }).unless({
    path: [
        { url: /^\/api\/members\/confirm\/.*/, methods: ['GET', 'PUT'] }
    ]
})


来源:https://stackoverflow.com/questions/30559158/handling-parameterised-routes-in-express-jwt-using-unless

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!