如何从自定义域提供 firebase 功能?

How do I serve firebase functions from a custom domain?

我有一个网站,我已经 运行 使用 google 域在 firebase 托管上。我现在想显示通过 url(例如 api.mydomain.com)而不是默认的 firebase 域对我的 firebase 函数进行的所有调用。我怎么可能做到这一点?

我阅读了 hosting cloud functions, and I also came across this article 上有关创建多个站点的 firebase 教程。那么有人可以告诉我如何设置工作流程,使我的网站在 mydomain.com 仍然是 运行,但我的 API 现在是通过 api.mydomain.com 调用的吗?

的目标名称是什么

如果可能,我希望所有请求都显示为对 api.mydomain.com 的请求,而不是对 api.mydomain.com/endpoint 的请求 - 这样也隐藏了被命中的端点来自 public

抱歉,我是新手。

假设您的主项目的 ID 为 example-app。要以 api.mydomain.com 的形式提供请求,您必须使用使用 express(或其他类似路由处理程序)的云函数。

  1. 使用 Firebase CLI 为您的项目创建辅助站点(ID 为 example-app-apiexample-api 等)
firebase hosting:sites:create example-app-api
  1. 将您的托管目标连接到您的资源
firebase target:apply hosting app example-app
firebase target:apply hosting api example-app-api
  1. 修改您的 firebase.json 文件以适应上述目标。
{
  "hosting": [
    {
      // app is linked to example-app, served as mydomain.com
      "target": "app",

      // contents of this folder are deployed to the site "example-app"
      "public": "public",

      // ... other settings ...
    },
    {
      // api is linked to example-app-api, served as api.mydomain.com
      "target": "api",

      // Contents of this folder are deployed to the site "example-app-api"
      // Any file here will be returned instead of calling your Cloud Function.
      // Recommended contents:
      //   - favicon.ico        (website icon for bookmarks, links, etc)
      //   - robots.txt         (instructions for bots and scrapers)
      // Optional contents:
      //   - service-worker.js  (empty file, used to prevent triggering cloud function)
      //   - humans.txt         (details about who you/your company are & how to report bugs)
      "public": "api-static-resources",  

      // ... other settings ...

      "rewrites": [
        {
          // redirect all calls to the function called "api"
          "source": "**",
          "function": "api"
        }
      ]
    }
  ]
}
  1. 使用 Firebase CLI 部署 api 托管配置
firebase deploy --only hosting:api
  1. 打开 Hosting Settings for your project, click "View" for example-app-api then click "Custom Domain" following these instructions

  2. 您现在应该可以通过在 api.mydomain.com 调用它来触发您的 Cloud Functions。

api.mydomain.com/getPost?id=someId
api.mydomain.com/favicon.ico
api.mydomain.com/robots.txt