我在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"
。怎么了?
答案 0 :(得分:3)
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。