表示4个参数条件
我想要有以下路线:
// Services without csrf() router.get('/user/:uid', userRes.findUser, userRes.GETUser); router.post('/user/:uid', userRes.findUser, userRes.POSTUser); // Rest of routes, with csrf() router.use(csrf()); router.post('/user/subscribe', indexRes.POSTSubscribe);
但是这里发生的是POST /user/subscribe
匹配第二条路线。
我一直在阅读Express路线参数条件,但它显示如何过滤数字。 我想过滤“订阅”path:
有没有机会?
你可以使用router.param
:
var staticUserPaths = ['subscribe']; router.param('uid', function (req, res, next, id) { if (~staticUserPaths.indexOf(id)) { next('route'); } else { next(); } });
如果你在/user/:uid
路由之前移动你的/user/subscribe
路由,它将被执行而不是/user/:uid
路由请求到/user/subscribe
。 Express中的路由/中间件按照它们所附的顺序执行。