如何在Firebase托管内部配置重写规则以将某些请求路由到云端功能? [英] How to configure rewrite rules inside firebase-hosting to route certain requests to cloud functions?

查看:125
本文介绍了如何在Firebase托管内部配置重写规则以将某些请求路由到云端功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用聚合物2.0和聚合物燃烧的PWA,是我的网络应用程序。我有一个快速的应用程序作为云功能(微服务)。
示例: exports.register = functions.https.onRequest(app);



如何添加将上面的应用程序映射为 / fns / register / fns / verify 的重写规则注册



我更新了cloudfunction microservice项目中的 firebase.json 文件,但是当我运行 firebase deploy --only函数:注册它说没有公用文件夹来部署托管配置!

  {
hosting:{
rewrites:[{
source :/ fns / **,function:register
}]
}
}

在原始web应用程序中维护重写规则可能是一种选择,但仍然不是理想的恕我直言。如果我必须在我的原始Web应用程序中执行此操作,我也尝试过,但无法完成。以下是我在原来的Web应用程序中更新的 firebase.json

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


解决方案

只为所有人维护一个项目资源(托管,功能和数据库)是理想的,我认为是正确的方式来管理Firebase项目。



您正试图改变一个参数(重写)托管服务,而不是它的工作方式。在部署firebase.json时,所有其他配置将被覆盖。所以,你得到的错误是因为Firebase没有查看最后一个配置文件,并检查更新的内容,它只是试图覆盖所有最后的配置文件,并得到一个错误,因为公共是托管的必要参数。 / p>

这就解释了,现在您希望Firebase将 / fns / register 重写为 /注册,但不会发生。你的函数会得到完整的url / fns / register

最好的办法是创建根路由:
$ b

  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);

将所有函数重写为 fns 函数:

  {
数据库:{
rules:database.rules.json
},
hosting:{
public:build / default / public,
rewrites:[
{
source :/ fns / **,
function:fns
},
{
source:**,
目的地:/index.html
}
]
}
}

现在您可以使用 https://< your-project-id> .firebaseapp.com / fns / register 到达您的注册功能和 https://< your-project-id> .firebaseapp.com / fns / verify 以达到您的验证功能。


I've a PWA built using polymer 2.0 and polymerfire and is my web application. I've an express app acting as a cloud function (microservice). Example: exports.register=functions.https.onRequest(app);

How to add the rewrite rules to map say /fns/register and /fns/verify to the above app register.

I've updated my firebase.json file in the cloudfunction microservice project, but when I run firebase deploy --only functions:register it says there is no public folder for deploying the hosting configuration!

{
    "hosting": {
        "rewrites": [{
            "source": "/fns/**", "function": "register"
        }]
    }    
}

Maintaining the rewrite rules in the original web applicaiton could be one option, but still, is not ideal IMHO. If I've to do it in my original web application, I tried that as well, but couldn't make it. Following is my updated firebase.json in my original web application:

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

解决方案

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.

这篇关于如何在Firebase托管内部配置重写规则以将某些请求路由到云端功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆