Max
Max

Reputation: 111

Param parsing twice for nested routes in Express

When I make GET http://localhost:8080/messages/3/sentiments for the code below why param method is called two times? So If I have 10 routes it will be called 10 times?

var comments = new Router();
comments.get('/comments', function (req, res, next) {
  res.send('Comments by message_id=' + req.message._id);
})

var sentiments = new Router();
sentiments.get('/sentiments', function (req, res, next) {
  res.send('Comments by message_id=' + req.message._id);
})

var messages = new Router();
messages.param('_message', function (req, res, next, _id) {
  console.log("Set message");
  fs.readFile(__filename, function () {
    req.message = { _id: _id };
    next();
  });
})

messages.use('/messages/:_message/', comments);
messages.use('/messages/:_message/', sentiments);

app.use(messages);
app.listen(8080);

Upvotes: 1

Views: 435

Answers (1)

Max
Max

Reputation: 111

Confirmed to be a bug in Express < 4.3

https://github.com/visionmedia/express/issues/2121

Upvotes: 1

Related Questions