我们有一个使用firebase和firestore作为数据库的应用程序。我们希望在我们的应用程序中实现几个部分,我们的客户使用应用程序中的现有数据来管理他们网站上的几个内容。我们希望为他们创建一个api来获取他们网站上的数据。但是当我们在api上提供数据时,我们不想直接从firestore获取数据,因为对于大多数静态内容来说,这是昂贵和不必要的。
我们想要创建一个无头cms模型,cms ui是我们的应用程序,但应该如何存储数据和服务。我们可以认为,当应用程序中的数据发生变化时,我们可以创建几个云函数来以所需的方式将数据保存到某个地方,并且我们可以将express.js节点应用程序部署到谷歌云应用程序引擎以提供应用程序编程接口。但是我们应该如何存储数据,或者我们是否有其他可以考虑的想法?
发布于 2021-10-28 20:15:11
我们想为他们创建一个应用程序接口来获取他们网站上的数据。但是当我们在api上提供数据时,我们不想直接从firestore获取数据,因为对于大多数静态内容来说,这是昂贵和不必要的。
要做到这一点,最直接的方法是结合使用Firebase的函数和主机。nodejs函数将为您提供一种直接从Firestore获取数据的方法。托管部分基本上会将来自Firestore的数据作为JSON提供,并提供适当的缓存时间。Firebase托管内置CDN,所以您的云函数只有在缓存过期时才会被调用。
const express = require("express");
const cors = require("cors");
const admin = require("firebase-admin");
const page = express();
page.use(cors());
page.get("/endpointName", (request, response) => {
return admin
.firestore()
.collection("name")
.get()
.then((docs) => {
// setting cache time to 7 days
const cacheTime = 604800;
response.set(
"Cache-Control",
`public, max-age=${cacheTime}, s-maxage=${cacheTime}`
);
// Do something with the firestore data -> docs
// Return the JSON response that you want:
response.send(...);
});
});
module.exports = page;下面是firebase.json文件可能的样子-
{
"hosting": {
"public": "public",
"rewrites": [
{
"source": "/endpointName",
"function": "page"
}
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
},
"functions": {
"source": "functions"
}
}https://stackoverflow.com/questions/67042349
复制相似问题