【发布时间】:2021-03-30 18:03:10
【问题描述】:
我的 MERN 应用的文件夹结构如下:
如您所见,在hamburger 目录中,有两个目录:client 和server。
在index.js 内部,即server 目录内部,我正在构造一个index.html 的绝对路径,它位于client\build 目录内部。请参阅下面突出显示的部分:
hamburger/server/index.js
const express = require("express");
const path = require("path");
const dotenv = require("dotenv");
const colors = require("colors");
const connectDB = require("./config/db");
const {
notFound,
globalErrorHandler,
} = require("./middleware/errorMiddleware");
const userRoutes = require("./routes/userRoutes");
const downloadRoutes = require("./routes/downloadRoutes");
dotenv.config();
connectDB();
const app = express();
app.use(express.json());
app.use("/api/users", userRoutes);
app.use("/api/download", downloadRoutes);
// Routing logic in production
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "../client/build")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
app.use(notFound);
app.use(globalErrorHandler);
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`.yellow.bold);
});
问题
目前绝对路径解析为server\client\build\index.html,这是错误的,因为client目录不在server目录内;它位于hamburger 目录中。
我想要什么?
我需要在path.resolve() 内进行哪些更改,以便获得index.html 的正确绝对路径,该路径位于client\build 目录内。
【问题讨论】: