How to configure rewrite rules inside firebase-hosting to route certain requests to cloud functions?

≯℡__Kan透↙ 提交于 2019-11-28 07:00:17

Maintaining just one project for all resources (Hosting, Functions and Database) is the ideal, I think is the right way to manage Firebase projects.

You are trying to change just one parameter (rewrites) of the hosting service, and it's not the way it works. When you deploy the firebase.json, all the others configurations are overwritten. So, the error you got is because Firebase don't look the last configuration file and check what's different to update, it just tries to overwrite all the last configuration file and get an error because "public" is a required parameter for hosting.

That explained, now you are expecting that Firebase rewrites /fns/register to just /register, but it'll not occur. Your function gonna receive the "full" url /fns/register.

The best way, I think, is to create a root route:

var functions = require('firebase-functions');
var express = require('express');

var app = express();
var router = express.Router();

router.post('/register', registerFunction);
router.post('/verify', verifyFunction);

app.use('/fns', router);

exports.fns = functions.https.onRequest(app);

And rewrites all functions to fns function:

{
  "database": {
    "rules": "database.rules.json"
  },
  "hosting": {
    "public": "build/default/public",
    "rewrites": [
      {
        "source": "/fns/**",
        "function": "fns"
      },
      {
        "source": "**",
        "destination": "/index.html"
      }
    ]
  }
}

Now you can use https://<your-project-id>.firebaseapp.com/fns/register to reach your register function and https://<your-project-id>.firebaseapp.com/fns/verify to reach your verify function.

This question is already answered here Firebase Hosting with dynamic cloud functions rewrites

I agree with you that it is best to keep the SPA in one project and the microservice in a different one but @Marcos V its correct about using a root function

I've up-voted for the answer from Marcos V. but I can't really accept that as an answer. Primarily because in microservice world you won't create a monolith with all the functionality in one place. You would rather break into manageable chunks and make as many appropriate microservices as necessary/ reasonable for the application.

With the current set-up with firebase-hosting, you have to have the host configuration file in one project alone. Also, the hosting shall be in one project because all the HTML, JS, CSS related to your site would have inter-dependencies and hence are inseparable (at least as of now).

Whereas cloud functions are better deemed as microservices serving a definitive purpose and hence needs to be in separate project/microservice as needed. Which can be deployed easily with the firebase deploy --only functions:YOUR_FN_NAME

Whenever you need to have a mapping added for the new cloud functions microservice, go ahead and make the routing change in the main hosting application and deploy the same. With this approach, we can at least have the back end part being microservices.

Now maintaining database rules in the same hosting application or a separate project is left to the designers who can decide based on their usecase.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!