gpt4 book ai didi

node.js - Express 中嵌套路由的参数解析两次

转载 作者:太空宇宙 更新时间:2023-11-04 01:05:09 25 4
gpt4 key购买 nike

当我为下面的代码创建 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 < 4.3 中的错误

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com