Search code examples
firebaseexpressgoogle-cloud-functionsfirebase-hosting

Firebase hosting custom domain 404 when forwarding requests to Cloud Functions


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


Solution

  • 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.