如何从自定义域提供 firebase 功能? [英] How do I serve firebase functions from a custom domain?

查看:28
本文介绍了如何从自定义域提供 firebase 功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a website that I'm already running on firebase hosting using a google domain. I would like to now show all calls to my firebase function being made through a url such as api.mydomain.com, instead of the default firebase domain. How is it that I may be able to do this?

我阅读了关于 托管云函数的 firebase 教程,我也偶然发现了关于创建多个网站的这篇文章.那么有人可以告诉我如何设置工作流程以使我的网站仍在 mydomain.com 上运行,但我的 API 现在正在通过 api.mydomain.com 调用?目标名称是什么

I read the firebase tutorial on hosting cloud functions, and I also came across this article on creating multiple sites. So could someone please tell how is it that I can set up the workflow such that my site is still running at mydomain.com, but my APIs are now being called through api.mydomain.com? What would be the target name for

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

If possible, I Would like all requests to be shown as requests to api.mydomain.com, and not to api.mydomain.com/endpoint - so that what endpoint is being hit is also hidden from public

对不起,我是新手.

推荐答案

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

Let's say your main project has an ID of example-app. To serve requests as api.mydomain.com, you would have to use a Cloud Function that makes use of express (or some other similar route handler).

  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 文件以适应上述目标.
  1. Modify your firebase.json file to suit the targets above.

{
  "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. 为您的项目打开 托管设置,点击查看"对于 example-app-api 然后单击自定义域";遵循这些说明.

  1. Open Hosting Settings for your project, click "View" for example-app-api then click "Custom Domain" following these instructions.

您现在应该能够通过在 api.mydomain.com 调用 Cloud Function 来触发它.

You should now be able to trigger your Cloud Function by calling it at api.mydomain.com.

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

这篇关于如何从自定义域提供 firebase 功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆