Firebase http 路由不起作用
Firebase http route doesn't work
我在 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"
。怎么了?
https://us-central1-company-1234.cloudfunctions.net/findprinters
URL 是该函数的正常 public API 端点或 "HTTP-trigger"。如果您要使用 Postman 向 URL 发出 GET 请求,您应该期望 运行 您的函数。 (或者,如果您在浏览器中访问了 URL,您的浏览器将向 URL 发出 GET 请求,然后您的函数也应该 运行)
当您想从 deployed/hosted 网站访问它时,问题就来了。您需要告诉 Firebase 的托管部分将 /findprinters
的任何流量路由到您的函数 - 您的 Firebase 托管应该 而不是 尝试将 /findprinters
路由解析为正常 HTML 页面与主要 index.html 文件一起部署...相反,它应该将 /findprinters
的所有流量引导至云功能 findprinters
因此,您需要告诉 Firebase 将 /findprinters
的任何流量定向到云功能,而不是托管。您在 firebase.json
文件中提供 Firebase commands/configuration...在这种情况下,在名为 "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。
我在 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"
。怎么了?
https://us-central1-company-1234.cloudfunctions.net/findprinters
URL 是该函数的正常 public API 端点或 "HTTP-trigger"。如果您要使用 Postman 向 URL 发出 GET 请求,您应该期望 运行 您的函数。 (或者,如果您在浏览器中访问了 URL,您的浏览器将向 URL 发出 GET 请求,然后您的函数也应该 运行)
当您想从 deployed/hosted 网站访问它时,问题就来了。您需要告诉 Firebase 的托管部分将 /findprinters
的任何流量路由到您的函数 - 您的 Firebase 托管应该 而不是 尝试将 /findprinters
路由解析为正常 HTML 页面与主要 index.html 文件一起部署...相反,它应该将 /findprinters
的所有流量引导至云功能 findprinters
因此,您需要告诉 Firebase 将 /findprinters
的任何流量定向到云功能,而不是托管。您在 firebase.json
文件中提供 Firebase commands/configuration...在这种情况下,在名为 "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。