Param在Express中针对嵌套路由解析两次

时间:2014-05-18 10:02:20

标签: node.js express routes

当我为下面的代码制作GET http://localhost:8080/messages/3/sentiments时,为什么param方法被调用两次?那么,如果我有10条路线,它将被调用10次?

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);

1 个答案:

答案 0 :(得分:1)

确认是Express<中的错误4.3

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