Rahul jhawar
Rahul jhawar

Reputation: 237

Skipping a middleware in express

I'm not looking for authorization of a particular request and so I did this.If the request path matches I want to skip the auth.verifyToken middleware.How can I do this.I tried using return next() but its not working.

 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);

Upvotes: 1

Views: 2576

Answers (1)

Harshal Yeole
Harshal Yeole

Reputation: 4983

next() is used to skip the middleware, you are just using it at the wrong place.

Try this code:

 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);

Upvotes: 1

Related Questions