Custom domain Firebase cloudfuntions API

Viewed 229

I have an Express API already in production using Firebase Cloud Functions but I need to add custom domain to it. I followed these instructions to create a custom domain for Cloud Functions using Firebase Hosting and end up with the following firebase.json

{
  "functions": {
    "predeploy": [
      "npm --prefix \"$RESOURCE_DIR\" run lint",
      "npm --prefix \"$RESOURCE_DIR\" run build"
    ],
    "source": "functions"
  },
  "firestore": {
    "rules": "firestore.rules",
    "indexes": "firestore.indexes.json"
  },
  "emulators": {
    "functions": {
      "port": 5001
    },
    "firestore": {
      "port": 8080
    },
    "ui": {
      "enabled": false
    }
  },
  "hosting": [{
    "site": "devbp",
    "target:":"devbp",
    "public": "public",
    "rewrites": [
      {
        "source": "/api/**",
        "function": "api"
      }
    ]
  }]
}

index.ts

import { functions } from './utils/exports-converter';
import App from './web-api/configs/app';

export * from './functions';
export * from './backup/schedules';
export * from './firestore-triggers/credits/credits-triggers'
export * from './schedules/transactions/aggregations-schedules';


export const api = functions.runWith({
    memory: '1GB',
    timeoutSeconds: 300
}).https.onRequest(App.express);

exports-converter.ts

import * as express from "express";
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";

export { express, functions, admin };

app.ts

import * as cors from "cors";
import * as bodyParser from "body-parser";
import Routes from "./routes";
import { express } from "../../utils/exports-converter";

class App {
  public express: express.Application;
  constructor() {
    this.express = express();
    this.init();
    this.mountRoutes();
  }
  private init() {
    const corsOptions = { origin: true };
    this.express.use(cors(corsOptions));
    this.express.use(bodyParser.raw());
    this.express.use(bodyParser.json());
    this.express.use(bodyParser.urlencoded({ extended: true }));
  }
  private mountRoutes() {
    Routes.mount(this.express);
  }
}

The problem is that the endpoints inside the api are not reachable, e.g. GET devbp.web.app/api/user?id=123 which returns Cannot GET /api/user. This response indicates that Express is handling the requests (if not, the Hosting would throw a 404 page) but not as expected.

I believe that I'm missing something in my firebase.json because the same api, as said above, is currently in production.

Any thoughts on it?

2 Answers

According to the documentation (here as well) hosting syntax is:

"hosting": {
...
}

while in the presented firebase.json you have [{ ... }]. Try to remove square brackets.

I am not sure, if I am following the logic, however I don't see any new App anywhere in the code. If I understand correctly if you do not create new App object you will not run it's constructor so express app will not be created.

2nd thing I think :) is that, again according to my understanding, there should be Express get() method called when GET request is called by http GET requst.

I found this tutorial it's not cloud function, however the logic is similar. New instance App is created and get method invoked.

Related