我想用云函数为我的 React 项目提供服务。但是使用下面的代码我得到错误 500:
服务器错误[错误:ENOENT:没有这样的文件或目录,打开'/workspace/build/index.html']
如何访问托管文件夹?
这是我的云功能:
const functions = require("firebase-functions");
const express = require("express");
const path = require("path");
const fs = require("fs").promises;
const app = express();
app.get("/*", async (req, res) => {
const filePath = path.resolve(__dirname, "./build", "index.html");
try {
let data = await fs.readFile(filePath, "utf-8");
data = data
.replace(/__TITLE__/g, "Gnossi Wohnig")
.replace(/__THUMB__/g, "")
.replace(/__DESCRIPTION__/g, "Genossenschaftswohnung in Zürich finden");
res.send(data);
} catch (error) {
res.sendStatus(500);
}
});
app.use(express.static(path.resolve(__dirname, "./build")));
exports.webApi = functions.https.onRequest(app);
需要将build文件夹复制到functions文件夹中。将以下内容放入 package.json 解决了问题:
"build": "react-scripts build && cp -r build functions",