Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by jfriend00 for Selectively apply middleware in express

The only way I know of to do that is to apply the first two middlewares directly to your router with no path prefix:

router.use(middleware1, middleware2);

Then, in each of those middlewares, check the path of the URL and if it is the special path that you want to skip those middlewares for, then just call next().

if (req.path.indexOf("/somepath") === 0) { return next() };

Then, you can register your third middleware only for the path you are interested in:

router.use("/somepath", middleware3);

The first two middewares will skip the ones you want them to skip and the third one will only get called for your specific route.


Viewing latest article 2
Browse Latest Browse All 3

Trending Articles