gpt4 book ai didi

express - 具有动态云功能的 Firebase 托管重写

转载 作者:行者123 更新时间:2023-12-03 21:08:54 25 4
gpt4 key购买 nike

我在 firebase 上有一个基于 express.js 的云函数应用程序,其函数名为 api .要使用自定义域,我正在尝试使用 Firebase 托管重写将特定 URL 路由到函数。
我在这里关注有关云功能和 Firebase 托管的官方文档,https://firebase.google.com/docs/hosting/functions ,并尝试了许多组合,包括:

"rewrites": [
{
"source": "/api/**",
"function": "api"
}
]

"rewrites": [
{
"source": "/api/:path1/:dat1/dat",
"function": "api/:path1/:dat1/dat"
}
]
"rewrites": [
{
"source": "/api/path1/dat1/dat",
"function": "api"
}
]
"rewrites": [
{
"source": "/api/*/*/*",
"function": "api"
}
]

可悲的是,它似乎不适用于任何可能的组合。
我的 express 应用程序有以下我计划使用的 GET 路径:
'/api/users/:userId/:userData'
'/api/users/:userId/:userData/json'
'/api/users/:userId/'

和其他类似的。 :userId 和 :userData 是我请求中的参数,因为它适用于 express.js

所需的功能按预期工作
https://my-firebase-app.cloudfunctions.net

但他们不与
https://my-app.firebaseapp.com

请告诉我这些应该如何工作以及我做错了什么。

编辑:
这是我的云函数导出的示例
const functions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
// Do App stuff here
}
// A couple more app.get in the same format

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

编辑2:
在@DougStevenson 的建议下,我尝试了以下配置

我在我的 firebase.json 中尝试了以下内容,
{
"hosting": {
"rewrites": [
{
"source": "/api",
"function": "api"
}
],
"public": "public"
}
}

但是我遇到了同样的问题,该函数永远不会被调用。
我读到了重写是最后的选择,如果主机中存在文件,它将不会转到指定的功能。(我尝试寻找提到这个 ws 的 SO 帖子,但我找不到它)所以我从主机的公共(public)目录中删除了 404.html 和 index.html 文件,因为无论如何我都不需要它们。但问题仍然存在。

编辑2:
好的,经过大量的试验和错误,我只需要按以下格式对路径进行硬编码:
rewrites : [
{
"source": "/users/**/**/json",
"function": "api"
},
{
"source": "/api/users/**/**/json",
"function": "api"
}
]

在此之后,快速应用程序配置如下:
app.get('/users/:userId/:userData/json', Foo)

我仍然希望有人可以提出更好的方法来完成此任务,而不是在托管重写中手动放入每个所需的 Uri。

最佳答案

似乎主要的问题是:

{
"source": "/api",
"function": "api"
}

实际上正在重写为 https://my-firebase-app.cloudfunctions.net/api/api而不是 https://my-firebase-app.cloudfunctions.net/api就像你期望的那样。注意 api重复。

我对此的解决方案是创建一个 main托管所有其他顶级函数的函数:
const functions = require('firebase-functions');
const express = require('express');
const app = express();

app.get('/users/:userId/:userData/json', (req, res) => {
// Do App stuff here
}
// A couple more app.get in the same format

// Create "main" function to host all other top-level functions
const main = express();
main.use('/api', app);

exports.main = functions.https.onRequest(main);

您现在可以使用 main在不破坏 URL 结构的情况下委托(delegate)给所有其他函数的函数:
{
"source": "/api/**", // "**" ensures we include paths such as "/api/users/:userId"
"function": "main"
}

瞧!您现在可以访问所有 api通过 https://my-app.firebaseapp.com/api/users/:userId/:userData 调用函数就像你期望的那样。

调用此端点,现在重写为 https://my-firebase-app.cloudfunctions.net/main/api这在技术上是正确的。然后,您可以添加更多顶级函数,只需将它们添加到您的 main如果您愿意,可以使用:
const hooks = express();
main.use('/hooks/, hooks);

关于express - 具有动态云功能的 Firebase 托管重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44959652/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com