【问题标题】:How do I serve firebase functions from a custom domain?如何从自定义域提供 firebase 功能?
【发布时间】:2021-04-11 00:18:59
【问题描述】:

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

我阅读了 hosting cloud functions 上的 firebase 教程,并且还遇到了 this article 创建多个站点。那么有人可以告诉我如何设置工作流程,以使我的网站仍在 mydomain.com 上运行,但我的 API 现在正在通过 api.mydomain.com 调用?目标名称是什么

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

对不起,我是新手。

【问题讨论】:

    标签: firebase google-cloud-functions firebase-hosting


    【解决方案1】:

    假设您的主项目的 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,点击example-app-api 的“查看”,然后点击these instructions 后面的“自定义域”。

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

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

    【讨论】:

    • 非常感谢!这是一个很棒的答案!我在第 3 步中有点困惑:我会为我的 node.js 函数修改 firebase.json 文件吗?还是针对实际网站?
    • 这是部署代码的项目目录中的firebase.json。如果您为托管站点使用名为 firebase.json 的文件(其中包含您的 appIddatabaseURL 等),Firebase 托管会在 /__/firebase/init.json/__/firebase/init.js 为您生成配置文件(请参阅 the docs了解更多信息)
    • 感谢您的澄清 - 但我已经直接从另一个后端代码部署,而我的实际网站来自一个单独的网站。在这种情况下,我应该为我的网站或我的 firebase 函数更改 firebase.json 文件吗?根据您的描述,网站正确吗?
    • 项目firebase.json 文件(位于$PROJECT_DIR/firebase.json)用于配置您使用firebase deploy 时的部署设置。虽然它用于配置您的项目,但它实际上并未与您的代码一起部署。在您的 Cloud Functions 文件夹(默认:$PROJECT_DIR/functions)中,您不需要任何 firebase.json - 您可以只使用 admin.initializeApp()(无参数)。在您的托管文件夹(默认:$PROJECT_DIR/public)中,您不需要firebase.json 文件,因为上面的init.json/init.js 会为您处理。
    • 如果您有两个不同的项目目录,那么您可以将上述更改放到您部署托管站点的firebase.json
    猜你喜欢
    • 1970-01-01
    • 2021-04-22
    • 2019-12-12
    • 1970-01-01
    • 1970-01-01
    • 2020-08-19
    • 2020-09-26
    • 2018-04-11
    相关资源
    最近更新 更多