Search code examples
firebasegoogle-cloud-functionsfirebase-hosting

Firebase functions return 404


I am trying to set up a custom domain for firebase functions. Custom domain already verified and works fine. My firebase functions run under us-central1:

enter image description here

here is my code for login function:

const functions = require("firebase-functions");
const express = require(`express`);
const cors = require(`cors`);
const admin = require(`firebase-admin`);
admin.initializeApp();

const firestore = admin.firestore();
const appLogin = express();
appLogin.use(cors({origin:true}));

appLogin.post("/", async(req:Request, res:Response)=>{
    functions.logger.log(`received`);
    const body = req.body;
    functions.logger.log(body);
    const result = await firestore.collection(DbCollections.USER).add(body);
    return res.status(201).send(JSON.stringify({"user_id":result.id}));
}); 


exports.login = functions.https.onRequest(appLogin);

nothing super fancy...

So, when I am using url from screenshot in postman - it works fine. But I want to use custom domain like next:

https://mydomai.com/api/login

Here is my firebase.json hosting section:

  "hosting": {
    "public": "public",
    "ignore": [
      "firebase.json",
      "**/.*",
      "**/node_modules/**"
    ],
    "rewrites": [
      {
        "source": "/api/login",
        "function": "login"
      }
    ]
  }

But when I am trying to reach login with custom domain - it returns me 404 without any valuable error message: enter image description here

enter image description here

Any ideas?


Solution

  • The source of a rewrite is not removed from the URL in the call to the function, so you likely want to change appLogin.post("/", ...) to appLogin.post("/api/login/", ...).