1
votes

I have an api set up on Google Cloud Functions (https://us-central1-myproject-name.cloudfunctions.net/api/v1/123/456/ical.ics). This works well, but I wish to set up a "friendly" domain name for the api. :)

According to Googles documentation this is seems easy, but I cannot figure out how to tell hosting to serve my api function. When navigating to my friendly name (https://api.mydomain.com/api/v1/123/456/ical.ics) I get error 404.

Cannot GET /api/v1/123/456/ical.ics

I have verified the domain and updated the firebase.json file with the below code according to documentation.

  {
    "source": "/api/**",
    "function": "api"
  }

A very simplified representation of my function

const app = express()
app.use(cors({ origin: true }))
const express = require('express')

app.get('/v1/:catId/:userId/ical', async (req, res) => {
  ...
}

app.post('/v1/mail', async (req, res) => {
  ...
}

exports.api = functions.region('us-central1').https.onRequest(app)

Why is my api not being served by the firebase hosting domains?

Kind regards /K

3

3 Answers

3
votes

Your Express routes need to exactly match the incoming URL, not just the wildcard part.

app.get('/api/v1/ical', async (req, res) => {
  ...
}

app.post('/app/v1/mail', async (req, res) => {
  ...
}

I believe you can also tell Express to mount an app at a prefix in order to apply the same prefix to all routes with the use() method.

2
votes

Note, for those who tried to deploy to other region than us-central1 and encountered this 404 error, using firebase functions from firebase is only supported on us-central1.

Refer: https://firebase.google.com/docs/hosting/functions

Important: Firebase Hosting supports Cloud Functions in us-central1 only.

1
votes

I'm not sure, but using the examples in the Firebase page worked for me after I cleared ALL browsing history. Hope that helps to anyone looking at this question in the future.