我想在node.js的快速框架中更改req.ip的值,但是req.ip仍然保持相同的值,而自定义的memeber ex:req.my_var工作;我无法弄清楚这两种情况的区别......有什么想法吗?
var truncateIPAddress = function (req, res, next) {
req.ip = req.ip.substring(0,30); // still the same value in other route
req.my_var = 'test'; //it works
next();
}
app.use(truncateIPAddress);
..........
app.use('/', index);
这是因为req.ip
使用Express在Node JS中提供客户端的IP地址。根据您的代码,您试图覆盖req.ip
的值。
这将适用于您的truncateIPAddress
函数,但由于其范围,它的覆盖值将再次重置。
因此,您必须选择全局变量并全局替换该值,并在所需位置上使用该全局变量。
谢谢!