如何从自定义域提供 firebase 函数?

sal*_*254 5 firebase firebase-hosting google-cloud-functions

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

我阅读了有关托管云功能的 firebase 教程,并且还看到了这篇有关创建多个站点的文章。那么有人可以告诉我如何设置工作流程,使我的网站仍在 mydomain.com 上运行,但现在通过 api.mydomain.com 调用我的 API 吗?目标名称是什么

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

抱歉,我对此很陌生。

sam*_*man 8

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

  1. 使用 Firebase CLI 为您的项目创建辅助站点(ID 为example-app-apiexample-api等)
firebase hosting:sites:create example-app-api
Run Code Online (Sandbox Code Playgroud)
  1. 将您的托管目标连接到您的资源
firebase target:apply hosting app example-app
firebase target:apply hosting api example-app-api
Run Code Online (Sandbox Code Playgroud)
  1. 修改您的firebase.json文件(位于 firebase 项目的根目录)以适应上述目标。
{
  "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"
        }
      ]
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)
  1. api使用 Firebase CLI部署托管配置
firebase deploy --only hosting:api
Run Code Online (Sandbox Code Playgroud)
  1. 打开项目的托管设置,单击“查看”,然后按照以下说明example-app-api单击“自定义域” 。

  2. 您现在应该能够通过调用 来触发您的云函数api.mydomain.com

api.mydomain.com/getPost?id=someId
api.mydomain.com/favicon.ico
api.mydomain.com/robots.txt
Run Code Online (Sandbox Code Playgroud)