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

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

问题描述

我有一个使用 polymer 2.0 和 polymerfire 构建的 PWA,是我的 Web 应用程序.我有一个充当云功能(微服务)的快速应用程序.示例:exports.register=functions.https.onRequest(app);

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

如何添加重写规则以将 /fns/register/fns/verify 映射到上述应用 register.

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

我已经在 cloudfunction 微服务项目中更新了我的 firebase.json 文件,但是当我运行 firebase deploy --only functions: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"
        }]
    }    
}

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

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"
      }
    ]
  }
}

推荐答案

只为所有资源(主机、函数和数据库)维护一个项目是理想的,我认为这是管理 Firebase 项目的正确方法.

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

您正试图仅更改托管服务的一个参数(重写),但它的工作方式并非如此.部署 firebase.json 时,所有其他配置都会被覆盖.所以,你得到的错误是因为 Firebase 没有查看最后一个配置文件并检查更新的不同之处,它只是试图覆盖所有最后一个配置文件并得到一个错误,因为public"是托管的必需参数.

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.

这解释了,现在您期望 Firebase 将 /fns/register 重写为 /register,但它不会发生.您的函数将收到完整"网址 /fns/register.

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

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

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"
      }
    ]
  }
}

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

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-hosting 中配置重写规则以将某些请求路由到云功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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