当我为下面的代码制作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);

最佳答案

确认是Express
https://github.com/visionmedia/express/issues/2121

关于node.js - Express中嵌套路由的参数解析两次,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23720753/

10-13 05:47