【发布时间】:2021-08-28 13:28:20
【问题描述】:
我正在尝试使用 javascript 从 firebase 函数中的客户端获取 IP 地址。为什么需要IP?将可以访问我的功能的 IP 列入白名单。
我的目标:通过 IP 地址访问我的功能
我尝试使用此方法,但对我不起作用,它给了我“未定义”:
exports.getIP = functions.https.onRequest(async (req, res) => {
var call = req.ip
console.log(call);
res.send(call);
});
exports.getIP = functions.https.onRequest(async (req, res) => {
var call = req.headers['x-forwarded-for']
console.log(call);
res.send(call);
});
当我调用 req.headers 时:
【问题讨论】:
-
x-forwarded-for是一个有效的标头,并且似乎正在工作。您也可以尝试使用x-appengine-user-ip标头。在最坏的情况下,你能console.log(req.headers)并分享输出吗? -
@Dharmaraj 我更新了问题,我尝试运行 'x-appengine-user-ip' 但它给了我未定义。
标签: javascript node.js firebase google-cloud-functions