Reputation: 1
the problem is when i define a middleware on a path with parameters the middleware also applies on other routes that dont have the param
i have this code:
app.use('/group/:groupId', groupmiddleware)
// This route should go through the groupMiddleware app.get('/groups/:groupId/another', (req, res) => {});
// Route that should not go through the groupMiddleware app.get('/groups/all', (req, res) => {});
now when I send a request to /groups/all it goes through the middleware when it shouldn't any advice or way to fix it would be appreciated. I am using express 4.18
Upvotes: 0
Views: 32
Reputation: 819
It all depends on the way you have defined the routes. You can define the middleware like this, so it will get used on that particular route.
Suppose if you want to use that middleware on this route only, then do this:
app.get('/groups/:groupId/another', [groupmiddleware], (req, res) => {})
So groupmiddleware
will only get used on this route.
Also sequence of routes also depends on express, define the routes in this sequence:
app.get('/groups/all', (req, res) => {})
app.get('/groups/:groupId', (req, res) => {})
app.get('/groups/:groupId/another', (req, res) => {})
Upvotes: 0