10

我正在使用我的 firebase 托管设置重定向(重写),以便我可以调用从 google cloud run here运行的 api 。

我尝试将重写字符串从"/api/**"(应该将所有内容捕获到 page.com/api/** 并将其发送到函数)。删除 index.html 并切换到"**"以捕获所有路径,包括 index.html。到目前为止没有任何效果。

我的托管 firebase.json 是这样设置的,这有什么问题吗?

{
  "hosting": {
    "public": "dist/public",
    "ignore": ["firebase.json", "**.*", "**/node_modules/**"],
    "rewrites": [
      {
        "source": "**",
        "run": {
          "serviceId": "next-js-base-api",
          "region": "us-central1"
        }
      }
    ]
  }
}

我也尝试过正常重定向到另一个页面,这不起作用,什么决定了 firebase.json 设置何时开始传播和工作?

更新

我尝试运行托管模拟器并进行了修改后的重写"source": "/api/**",结果如下。导航到 /api 返回非崩溃(不重定向),并在浏览器中输出cannot GET /api导航到 api/wkapi(由 api 端点捕获的子目录)unexpected error在浏览器中返回

Error: Unable to find a matching rewriter for {"source":"/api/**","run":{"serviceId":"next-js-base-api","region":"us-central1"}}

在控制台中。

4

3 回答 3

5

确保通过运行以下命令更新到最新版本的 Firebase CLI:

npm install -g firebase-tools@latest

这将使您能够按照您的尝试重写云运行实例。

于 2019-05-01T12:19:49.890 回答
1

其实我刚才跑了这个,查看部署的cloud-run helloworld容器的日志,发现custom-domain/helloworld实际上是映射到container-domain/helloworld,而不是简单地映射到container-domain/。为了解决这个问题,我必须在我原来的 Node.js 程序中添加一个额外的 app.get 规则:

app.get('/helloworld', (req, res) => {

然后调用 custom-domain/helloworld 工作。

于 2019-09-15T02:45:13.210 回答
0

如果以上所有答案都对您没有帮助,那么对我有用的解决方法是:

  • 查看项目中 firebase 托管使用的公共目录。
  • 删除 firebase 在执行 firebase 初始化步骤时创建的 index.html 文件。

删除生成的 index.html 文件后,它能够通过重写运行我的云运行容器。

于 2021-12-28T03:35:09.770 回答