Google 云函数没有被触发
Google cloud function don't get triggered
我试图在到达我的托管网站的路径时触发 google 云功能。
所以,我在我的 firebase.json
中添加了这个
"rewrites": [
{
"source": "**",
"destination": "/index.html",
"function": "app"
}
这是我的函数 "app":
[...]
server.get('*', (req:any,res:any) => {
const isBot = detectBot(req.headers['user-agent']);
if(isBot) {
const botUrl = generateUrl(req);
nf(`${renderUrl}/${botUrl}`)
.then((r: { text: () => any; }) => r.text())
.then((body: { toString: () => any; }) => {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.set('Vary','User-Agent');
res.send(body.toString())
});
} else {
nf(`https://${appUrl}`)
.then((r: { text: () => any; }) => r.text())
.then((body: { toString: () => any; }) => {
res.send(body.toString());
});
}
});
exports.app = functions.https.onRequest(server);
"app" 功能和网站已部署,但当我到达 url "app" 函数不会被触发。
提前致谢。
您的 "rewrites" 部分设置不正确。您指定目标(“/index.html”)和函数参数。这是一个重写示例,它将把对 /function-url
的请求定向到您的 app
函数,并将其他请求(未定义或“/foo”和“/foo/**”)定向到您的 index.html
:
"rewrites": [ {
// Serves index.html for requests to files or directories that do not exist
"source": "**",
"destination": "/index.html"
}, {
// Serves index.html for requests to both "/foo" and "/foo/**"
// Using "/foo/**" only matches paths like "/foo/xyz", but not "/foo"
"source": "/foo{,/**}",
"destination": "/index.html"
}, {
// calls your app function for requests to "/function-url"
"source": "/function-url",
"function": "app"
} ]
有关如何配置重写的详细信息here。
我试图在到达我的托管网站的路径时触发 google 云功能。
所以,我在我的 firebase.json
中添加了这个"rewrites": [
{
"source": "**",
"destination": "/index.html",
"function": "app"
}
这是我的函数 "app":
[...]
server.get('*', (req:any,res:any) => {
const isBot = detectBot(req.headers['user-agent']);
if(isBot) {
const botUrl = generateUrl(req);
nf(`${renderUrl}/${botUrl}`)
.then((r: { text: () => any; }) => r.text())
.then((body: { toString: () => any; }) => {
res.set('Cache-Control', 'public, max-age=300, s-maxage=600');
res.set('Vary','User-Agent');
res.send(body.toString())
});
} else {
nf(`https://${appUrl}`)
.then((r: { text: () => any; }) => r.text())
.then((body: { toString: () => any; }) => {
res.send(body.toString());
});
}
});
exports.app = functions.https.onRequest(server);
"app" 功能和网站已部署,但当我到达 url "app" 函数不会被触发。
提前致谢。
您的 "rewrites" 部分设置不正确。您指定目标(“/index.html”)和函数参数。这是一个重写示例,它将把对 /function-url
的请求定向到您的 app
函数,并将其他请求(未定义或“/foo”和“/foo/**”)定向到您的 index.html
:
"rewrites": [ {
// Serves index.html for requests to files or directories that do not exist
"source": "**",
"destination": "/index.html"
}, {
// Serves index.html for requests to both "/foo" and "/foo/**"
// Using "/foo/**" only matches paths like "/foo/xyz", but not "/foo"
"source": "/foo{,/**}",
"destination": "/index.html"
}, {
// calls your app function for requests to "/function-url"
"source": "/function-url",
"function": "app"
} ]
有关如何配置重写的详细信息here。