Sahand
Sahand

Reputation: 8370

Firebase http route doesn't work

I have defined the following route in firebase:

// ONLY FOR TESTING
exports.findprinters = functions.https.onRequest((req, res) => {
  console.log("Finding printers");
  findGooglePrinters();
});

I have deployed:

$ 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

But the route does not seem to be working. I go to https://company-1234.firebaseapp.com/findprinters but it doesn't work. The console.log does not log "Finding printers" like I expect it to. What's wrong?

Upvotes: 0

Views: 605

Answers (1)

JeremyW
JeremyW

Reputation: 5272

https://us-central1-company-1234.cloudfunctions.net/findprinters

That URL is the normal public API endpoint or "HTTP-trigger" for that function. If you would use Postman to make a GET request to that URL, you should expect to run your function. (Or if you visited that URL in your browser, your browser would make a GET request to that URL, and your function should run then too)

The problem comes when you want to access it from your deployed/hosted website. You need to tell the hosting portion of Firebase to route any traffic for /findprinters to your function - your Firebase hosting should not try to resolve the /findprinters route as a normal HTML page deployed along-side the primary index.html file... instead it should direct any traffic for /findprinters to the cloud function findprinters

So, you need to tell Firebase to direct any traffic for /findprinters to the cloud function, not hosting. You give Firebase commands/configuration in the firebase.json file... in this case, under a section named "rewrites" like this:

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

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

  }
}

Check out this documentation link where it explains all that^^

Once you've done that, redeploy everything, and now you should be able to visit /findprinters in your browser and trigger the function. NOTE Unless you are using firebase serve you should visit the route on the deployed website, not localhost. Check out this link for more details on firebase serve.

Upvotes: 3

Related Questions