There are many examples of using Express for Firebase Cloud Functions.
In every example of I have found the code exposes the Express app as a single Cloud Function:<
Interesting discussion.
I choose the same approach : one "endpoint" (aka a root route like "/posts", "/users") == a dedicated cloud function (for the reason already evoked + it's more "µservice like" and it's what are "lambda functions" for me).
To be "DRY" all my function import an "express" generator. I configure my express instance in one place.
const express = () => {
const express = require("express");
const cors = require("cors")({ origin: true });
const morgan = require("morgan");
const helmet = require("helmet");
const app = express();
app.use(helmet());
app.use(helmet.noCache());
app.use(cors);
app.use(morgan("combined"));
return app;
};
module.exports = express;
My "hello" endpoint :
const app = require("./../../express")();
/**
* /hello
*/
app.get("/", (req, res) => {
return res.send("Hello World");
});
module.exports = app;
My index.js (main export) :
const helloApi = require("./api/hello");
const https = functions.region("europe-west1").https;
module.exports.hello = https.onRequest(helloApi);
Seems to work well for us :)