当通过云功能访问时,如何从Firebase主机返回基本响应
我正在尝试将自定义当通过云功能访问时,如何从Firebase主机返回基本响应,firebase,google-cloud-functions,json-ld,Firebase,Google Cloud Functions,Json Ld,我正在尝试将自定义json+ld放到我的Firebase站点上。我试图通过使用云函数拦截(“重写”)对我的站点的请求来实现这一点,通过Fetch API获取一些json+ld信息,然后将这些信息添加到内容Firebase中,通常会以原始路径提供服务 有没有办法获得Firebase提供的“base”请求,并在其上附加一些内容 这是我的密码: exports.eventJsonLd = functions.https.onRequest((req, res) => { if (req.
json+ld
放到我的Firebase站点上。我试图通过使用云函数拦截(“重写”)对我的站点的请求来实现这一点,通过Fetch API获取一些json+ld
信息,然后将这些信息添加到内容Firebase中,通常会以原始路径提供服务
有没有办法获得Firebase提供的“base”请求,并在其上附加一些内容
这是我的密码:
exports.eventJsonLd = functions.https.onRequest((req, res) => {
if (req.path == '/') {
fetch('http://pathtoMyApi?clientIp=' + req.ip).then(resp => {
if (resp.ok) {
return resp.json();
}
throw new Error('Could not retrieve json+ld');
})
.then(x => {
//*** right here is where I would like to take this script tag
//*** and insert it into the body of the normal request to this path
res.send('<script type="application/ld+json">' + JSON.stringify(x[0]).replace(/typeString/g, '@type').replace(/contextString/g, '@context') + '</script>');
})
.catch(err => {
console.log('Fetch error: ' + err.message);
res.status(500).send(err.message);
});
}
});
exports.eventJsonLd=functions.https.onRequest((req,res)=>{
如果(请求路径=='/')){
取('http://pathtoMyApi?clientIp=“+req.ip)。然后(resp=>{
如果(分别正常){
返回resp.json();
}
抛出新错误('无法检索json+ld');
})
.然后(x=>{
//***这里就是我想要使用这个脚本标签的地方
//***并将其插入到此路径的正常请求主体中
res.send(“”+JSON.stringify(x[0]).replace(/typeString/g,'@type')).replace(/contextString/g,'@context')+“”);
})
.catch(错误=>{
log('Fetch error:'+err.message);
资源状态(500)。发送(错误消息);
});
}
});
有什么线索吗?谢谢
更新:
如果我做类似的事情呢
<script type='application/ld+json' src='http://path-to-my-cloud-function/'></script>
??它现在对我不起作用。但这样的解决方案可以吗?如何定义“基本请求”?我不确定您试图实现什么。这意味着,返回通常会到达
'/'
端点的响应,但添加一段从云函数动态检索的json。如果您重写宿主URL以转到云函数,则必须从函数内生成整个响应。你不能从一个函数内的主机访问文件,除非你对一个函数发出单独的HTTP请求,这听起来不是一件适合每个请求的好事情。谢谢@DougStevenson。在这种情况下,如何处理在页面加载时动态提供的json+ld?我正在尝试找出如何使动态生成的内容与我的标准托管firebase文件混合。