Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Firebase http路由不';行不通_Javascript_Firebase_Express_Google Cloud Functions_Firebase Hosting - Fatal编程技术网

Javascript Firebase http路由不';行不通

Javascript Firebase http路由不';行不通,javascript,firebase,express,google-cloud-functions,firebase-hosting,Javascript,Firebase,Express,Google Cloud Functions,Firebase Hosting,我在firebase中定义了以下路线: // ONLY FOR TESTING exports.findprinters = functions.https.onRequest((req, res) => { console.log("Finding printers"); findGooglePrinters(); }); 我已部署: $ firebase deploy --only functions === Deploying to 'company-1234'...

我在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
不像我期望的那样记录
“查找打印机”
。怎么了

https://us-central1-company-1234.cloudfunctions.net/findprinters
该URL是该函数的普通公共API端点或“HTTP触发器”。如果您要使用Postman对该URL发出GET请求,那么您应该希望运行您的函数。(或者,如果您在浏览器中访问了该URL,您的浏览器将向该URL发出GET请求,然后您的函数也应该运行)

当您希望从部署/托管的网站访问它时,就会出现问题。您需要告诉Firebase的托管部分将
/findprants
的任何流量路由到您的功能-您的Firebase托管应该而不是尝试将
/findprants
路由解析为沿主index.HTML文件部署的普通HTML页面。。。相反,它应该将
/findprants
的任何流量定向到云功能
findprants

因此,您需要告诉Firebase将
/findprants
的任何流量定向到云功能,而不是主机。您可以在
Firebase.json
文件中提供Firebase命令/配置。。。在本例中,在名为“重写”的部分下,如下所示:

{
  "hosting": {
    "public": "public",

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

  }
}
看看它是怎么解释的^^

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

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

当您希望从部署/托管的网站访问它时,就会出现问题。您需要告诉Firebase的托管部分将
/findprants
的任何流量路由到您的功能-您的Firebase托管应该而不是尝试将
/findprants
路由解析为沿主index.HTML文件部署的普通HTML页面。。。相反,它应该将
/findprants
的任何流量定向到云功能
findprants

因此,您需要告诉Firebase将
/findprants
的任何流量定向到云功能,而不是主机。您可以在
Firebase.json
文件中提供Firebase命令/配置。。。在本例中,在名为“重写”的部分下,如下所示:

{
  "hosting": {
    "public": "public",

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

  }
}
看看它是怎么解释的^^

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