【问题标题】:Google cloud function don't get triggered谷歌云功能不会被触发
【发布时间】:2020-04-30 10:57:14
【问题描述】:

当到达我的托管网站路径时,我正在尝试触发谷歌云功能。

所以,我在我的 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" 功能不会被触发。

提前致谢。

【问题讨论】:

    标签: node.js firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    您的“重写”部分设置不正确。您同时指定目标 ("/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的信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-31
      • 2021-07-04
      • 2017-11-14
      • 2018-02-21
      • 2021-11-05
      • 2020-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多