代码之家  ›  专栏  ›  技术社区  ›  Sahand

firebase http路由不工作

  •  0
  • Sahand  · 技术社区  · 8 年前

    我在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
    

    enter image description here

    但这条路线似乎行不通。我去 https://company-1234.firebaseapp.com/findprinters 但没用。这个 console.log 不记录 "Finding printers" 就像我期望的那样。发生了什么?

    1 回复  |  直到 8 年前
        1
  •  3
  •   JeremyW    8 年前
    https://us-central1-company-1234.cloudfunctions.net/findprinters
    

    该url是该函数的普通公共api端点或“http触发器”。如果要使用postman对该url发出get请求,则应该运行函数。( 或者,如果您在浏览器中访问了该url,浏览器将对该url发出get请求,然后您的函数也将运行 )

    当您希望从部署/托管的网站访问它时,就会出现问题。您需要告诉firebase的宿主部分为 /findprinters 你的功能-你的firebase主机应该 尝试解决 /金融中介 路由为一个普通的HTML页面,部署在主index.html文件旁边…相反,它应该为 /金融中介 到云函数 findprinters

    所以,你需要告诉firebase指挥 /金融中介 到云功能,而不是托管。在 firebase.json 文件…在本例中,在一个名为“重写”的部分下,如下所示:

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

    退房 this documentation link 它解释了这一切^^

    一旦你做到了,重新部署一切,现在你应该可以访问 /金融中介 在浏览器中并触发函数。 注意 除非你在使用 firebase serve 你应该在 部署的网站 ,而不是本地主机。退房 this link for more details 火力基地服务 是的。