我想只在请求来自Google bot用户代理时才调用firebase函数。
目前我对每个http请求都调用该函数。
response.send("hello google")
request({uri: "http://example.com/index.html"},
function(error, response, body) {
res.send(body)
});
});
这将导致一个循环,因为该函数将要求从firebase托管的index.html文件,这样做将再次调用该函数。所以我认为最好是调用该函数,只有当请求来自谷歌用户代理。
有什么办法可以解决这个问题吗,谢谢。
Google bot发出的请求有特定的用户代理,几乎所有的用户代理都包含关键字Googlebot。只要得到头,检查它是否包含关键字.假设你使用的是express。
if (req.get('User-Agent').toLowerCase().includes('googlebot')) {
res.send("hello google")
} else {
request({uri: "http://example.com/index.html"},
function(error, response, body) {
res.send(body)
});
});
//or res.redirect(302, "http://example.com/index.html")
}
https:/developers.whatismybrowser.comuseragentsexploresoftware_namegooglebot。https:/support.google.comwebmastersanswer1061943