Firebase云功能url不是来自前端的请求的基本url

时间:2020-02-24 06:11:57

标签: node.js reactjs firebase google-cloud-firestore axios

我正在使用ReactJS在前端构建一个Web应用程序,并使用Node and Express和Firestore从后端检索数据。

我正在将axios用于HTTP请求。我已经使用Problem Example Table: Name | mainv | Susan | 9 | Savannah| 9 | Trevor | 0 | //It is not sync with the rest that already set to 9 设置了baseurl。这是我所有云功能的终点。这些功能包括获取/发布,以获取或更新存储在Firestore中的某些数据。

它们都使用express导出为一个函数:axios.default.baseURL = "https://us-central1-<project-id>.cloudfunctions.net/api"

我的firebase.json重写如下:

exports.api = functions.https.onRequest(app);

问题是,在我部署的站点上,一些axios http调用转到了我设置为默认值的适当baseUrl。其他人转到我的托管网址:"hosting": { "public": "build", "ignore": ["firebase.json", "**/.*", "**/node_modules/**"], "rewrites": [ { "source": "**", "function": "api" } ] }

是否存在我未正确执行的操作?我希望所有http请求都转到正确的基本url,但似乎无法正常工作。

1 个答案:

答案 0 :(得分:1)

使用托管配置时,用于调用API的基本URL应该为<project-id>.firebaseapp.com/。如果您想访问自己的Cloud Function的/getData快递路线,请向<project-id>.firebaseapp.com/getData发送请求。

通常,您尝试避免直接调用cloudfunctions.net域,但要警惕将Cloud Function放在Firebase Hosting后面还会将CDN引入到管道中。 CDN将缓存结果并调用您的Cloud Function,同时丢弃非__session的cookie。

为防止不必要地将所有请求传递给您的Cloud Function,我将更改您的配置,以便仅在路径/api上调用您的Cloud Function。

"hosting": {
  "public": "build",
  "ignore": ["firebase.json", "**/.*", "**/node_modules/**"],
  "rewrites": [
    {
      "source": "api/**",
      "function": "api"
    }
  ]
}

这将需要将您的特快航线更改为/api/getData,等等。