在快递中跳过中间件

时间:2018-06-09 08:49:09

标签: node.js express

我没有寻找特定请求的授权,所以我这样做。如果请求路径匹配,我想跳过return next()中间件。我怎么能这样做。我尝试使用{{ 1}}但它不起作用。

 eventRouter.param('pin', (req, res, next, pin) => {
        let path = `/event/matchpin/${pin}`;
         if(req.path === path){
            //do something here so that directly executes the route
         }
         //else it executes the auth.verifyToken middleware
        next();
      });
    app.use('/user',auth.verifyToken,eventRouter);

1 个答案:

答案 0 :(得分:1)

next()用于跳过中间件,你只是在错误的地方使用它。

试试这段代码:

 eventRouter.param('pin', (req, res, next, pin) => {
        let path = `/event/matchpin/${pin}`;
         if(req.path === path){
            // Next will by pass this middleware
            next();
         }
         //else it executes the auth.verifyToken middleware

      });
    app.use('/user',auth.verifyToken,eventRouter);