我在 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 触发器”。如果您使用 Postman 向该 URL 发出 GET 请求,您应该期望运行您的函数。 (或者,如果您在浏览器中访问了该 URL,您的浏览器将向该 URL 发出 GET 请求,并且您的函数也应该运行)
当您想从部署/托管的网站访问它时,就会出现问题。您需要告诉 Firebase 的托管部分将 /findprinters
的任何流量路由到您的函数 - 您的 Firebase 托管不应不尝试解析 /findprinters
code> 路由作为与主 index.html 文件一起部署的普通 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
并触发该函数。 注意除非您使用firebaseserve
,否则您应该访问部署的网站上的路由,而不是本地主机。查看this link for more details在 firebase 服务
上。
关于javascript - Firebase http 路由不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51311838/