【问题标题】:Not all headers are shown with Firebase onRequest并非所有标头都与 Firebase onRequest 一起显示
【发布时间】:2019-11-27 23:00:49
【问题描述】:

我在做一个小项目时尝试学习 Firebase,我需要在我的系统中存储 IP 地址。

但我意识到它不像我以前使用 Express 那样工作。 我需要这个 Gist 的完全相同的结果:https://gist.github.com/katowulf/6fffffb45ee5cbfbca6c3511e5d19528#gistcomment-2674393

但是做同样的事情对我不起作用,我没有得到这么多的标头,也没有得到客户端 IP 地址。

exports.getPolls = functions.region('europe-west1').https.onRequest((req, res) => {
    console.log(util.format(req.headers))
});

我得到的标题

headers:
   { host: 'localhost:5001',
     connection: 'keep-alive',
     'upgrade-insecure-requests': '1',
     'user-agent':
      'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3370.152 Safari/537.36',
     accept:
      'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3',
     'accept-encoding': 'gzip, deflate, br',
     'accept-language': 'en,nl;q=0.9,tr;q=0.8',
     cookie: 'io=AVNO7ouAHOYCkVK8ADSD',
     'if-none-match': 'W/"5-qvTGHdzV6LKavt4PO0gs2a6pQ00"' 
    },

我需要客户端 IP 地址

【问题讨论】:

  • 什么是console.log(req.headers)console.log(req.headers['fastly-client-ip'])打印出来的?
  • @Nathan console.log(req.headers) 只是打印我在“我得到的标题”上面提到的内容。 console.log(req.headers['fastly-client-ip']) 打印出 undefined

标签: node.js firebase express google-cloud-firestore google-cloud-functions


【解决方案1】:

试试这个:

exports.myip = functions.https.onRequest((req, res) => { console.log(req.headers['x-forwarded-for']); return res.status(200).send(req.headers['x-forwarded-for'].toString()); });

【讨论】:

  • 我的req.headers 没有,所以它会返回undefined
  • @buryo 请部署 myip 函数并检查它是否返回 undefined
【解决方案2】:

这个问题已经解决了: https://github.com/firebase/firebase-functions/issues/541

简短说明:

在执行firebase serve 时,请求标头来自没有预期标头的模拟器,firebase deploy 是您需要做的。

转到firebase console > 功能(选择您的功能)> 日志,当您像我一样控制台日志时,将显示预期的标题(带有x-forwarded-for 标题)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-28
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多