【问题标题】:Firebase http route doesn't workFirebase http路由不起作用
【发布时间】:2018-12-21 01:13:02
【问题描述】:

我在 firebase 中定义了以下路由:

// ONLY FOR TESTING
exports.findprinters = functions.https.onRequest((req, res) => {
  console.log("Finding printers");
  findGooglePrinters();
});

我已经部署了:

$ firebase deploy --only functions

=== Deploying to 'company-1234'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
✔  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (47.21 KB) for uploading
✔  functions: functions folder uploaded successfully
i  functions: creating function findprinters...
i  functions: updating function quarterly_job...
i  functions: updating function createNewUser...
i  functions: updating function createStripeCharge...
i  functions: updating function createStripeCustomer...
i  functions: updating function addPaymentSource...
i  functions: updating function cleanupUser...
✔  functions[createStripeCharge]: Successful update operation. 
✔  functions[addPaymentSource]: Successful update operation. 
✔  functions[createStripeCustomer]: Successful update operation. 
✔  functions[cleanupUser]: Successful update operation. 
✔  functions[createNewUser]: Successful update operation. 
✔  functions[findprinters]: Successful create operation. 
Function URL (findprinters): https://us-central1-company-1234.cloudfunctions.net/findprinters
✔  functions[quarterly_job]: Successful update operation. 

✔  Deploy complete!

Project Console: https://console.firebase.google.com/project/company-1234/overview

但该路线似乎不起作用。我去https://company-1234.firebaseapp.com/findprinters,但它不起作用。 console.log 没有像我期望的那样记录 "Finding printers"。怎么了?

【问题讨论】:

    标签: javascript firebase express google-cloud-functions firebase-hosting


    【解决方案1】:
    https://us-central1-company-1234.cloudfunctions.net/findprinters
    

    该 URL 是该函数的普通公共 API 端点或“HTTP 触发器”。如果您使用 Postman 向该 URL 发出 GET 请求,您应该期望运行您的函数。 (或者,如果您在浏览器中访问了该 URL,您的浏览器会向该 URL 发出 GET 请求,然后您的函数也应该运行

    当您想从部署/托管的网站访问它时,问题就来了。您需要告诉 Firebase 的托管部分将 /findprinters 的任何流量路由到您的函数 - 您的 Firebase 托管应该尝试将 /findprinters 路由解析为同时部署的普通 HTML 页面主 index.html 文件...相反,它应该将 /findprinters 的任何流量定向到云功能 findprinters

    因此,您需要告诉 Firebase 将 /findprinters 的所有流量定向到云功能,而不是托管。您在 firebase.json 文件中提供 Firebase 命令/配置...在这种情况下,在名为“rewrites”的部分下,如下所示:

    {
      "hosting": {
        "public": "public",
    
        // Add the following rewrites section *within* "hosting"
        "rewrites": [ {
          "source": "/findprinters", "function": "findprinters"
        } ]
    
      }
    }
    

    查看this documentation link,它解释了所有这些^^

    完成后,重新部署所有内容,现在您应该可以在浏览器中访问/findprinters 并触发该功能。 注意 除非您使用firebase serve,否则您应该访问已部署网站 上的路由,而不是本地主机。在firebase serve 上查看this link for more details

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      • 2017-10-31
      • 2019-11-03
      • 2021-11-04
      相关资源
      最近更新 更多