Express: req.params in middleware

Created on 30 Apr 2014  Â·  9Comments  Â·  Source: expressjs/express

req.params are not available anymore in the middleware. However it is still available in the router.

eg:

app.use(function(req, res, next){
    console.log(req.params.country); //not valid
        next();
});

app.get('/:country', function(req, res, next){
     console.log(req.params.country); // valid
     next();
});

Most helpful comment

Sorry for bringing this up again. I wrote security middleware to check various things on incoming requests, one of those things is the params. It works when I put it with the route, but does not work at an 'app.use' level. Is there anyway possible that I can make this work without having to put it on each route? It would be so much cleaner to maintain this way.

All 9 comments

By design.

@defunctzombie

Can you suggest alternative for this? I need a generic middleware which will use the req.param.

Params can only be known if the route definition has params. Your example
doesn't make sense to me because it has middleware with no route. Obviously
there will be no params.
On Apr 30, 2014 10:20 AM, "Chirag" [email protected] wrote:

@defunctzombie https://github.com/defunctzombie

Can you suggest alternative for this? I need a generic middleware which
will use the req.param.

—
Reply to this email directly or view it on GitHubhttps://github.com/visionmedia/express/issues/2088#issuecomment-41801694
.

Hmm. Got it. I was doing it wrong actually.

@defunctzombie is it possible to read req.params if you associate it with a particular mount point ?
ie.. please check my question here. http://stackoverflow.com/questions/25319731/cannot-get-request-params-in-express-middleware/25319790#25319790

//middleware
function userMiddleware(req, res, next) {
  console.log('user came in. Hello ' + req.param('name'));
  next();
}

//register middleware
app.use('/user', userMiddleware)

// routes
app.get('/user/:name', function(req, res) {
  res.send('username : ' + req.params.name);
});

@gprasant That will not work either because the middleware will run before the route which is again where the params will be identified (from the route string). What you want in this case is to use app.param('name', function(...)) to run some logic possibly loading the name, etc.

Alternatively. app.route('/user/:name').all(function() { ... console log here }).get(function() { ... }) should work.

:thumbsup:

Sorry for bringing this up again. I wrote security middleware to check various things on incoming requests, one of those things is the params. It works when I put it with the route, but does not work at an 'app.use' level. Is there anyway possible that I can make this work without having to put it on each route? It would be so much cleaner to maintain this way.

+1

This would be very handy to have, but I get why it works the way it does for .use(). Perhaps .param() could accept a wildcard or regex for the name.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Domiii picture Domiii  Â·  3Comments

AndrewEQ picture AndrewEQ  Â·  4Comments

wxs77577 picture wxs77577  Â·  3Comments

despairblue picture despairblue  Â·  3Comments

gaurav5430 picture gaurav5430  Â·  3Comments