Passing req.params to next() 404 handler

Im using Node and Express 4. I have this code in a route:

db.clients.findById(req.params.id, function(err, client) {
    if (err) return next(err);
    if (!client) return next();
    res.json(client);
});

After all my routes I have this 404 handler.

/// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

So when no client is found, next() is called, which is the 404 handler. This works well, but all 404 messages are "Not Found". I would like to have more detailed messages, like "req.params.id was not found". But it seems req.params is not passed on to the 404 handler.

Q1: Why isn't the whole req passed on when using next()? I thought it was supposed to.

Q2: Is there a way to pass the req.params to the 404 handler?

The answers to your questions are:

  1. It is, but req.params is special because it contains the route-specific values from the URL. So if you had two route patterns that matched the same request but had different parameters, you wouldn't expect to see parameter values for the first route in your second route handler.

  2. You could attach the values under some other name, such as req._params = req.params;. Then use req._params.id in your 404 handler.