req.ip不能在快递中更改

问题描述 投票:0回答:1

我想在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);
javascript node.js express
1个回答
1
投票

这是因为req.ip使用Express在Node JS中提供客户端的IP地址。根据您的代码,您试图覆盖req.ip的值。

这将适用于您的truncateIPAddress函数,但由于其范围,它的覆盖值将再次重置。

因此,您必须选择全局变量并全局替换该值,并在所需位置上使用该全局变量。

谢谢!

© www.soinside.com 2019 - 2024. All rights reserved.