无法使用路径模块构建正确的绝对路径
Unable to construct the right absolute path using path module
我的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
目录中。
由于您的 index.js
位于 server
文件夹中,因此绝对路径将始终如 server\client\build\index.html
.
要解决您的问题,您必须更改文件夹结构或手动构建路径。
您必须添加 ../
以将 1 个目录向上移动到您的客户端文件夹所在的位置以构建正确的路径。
path.resolve(__dirname, "../client", "build", "index.html")
path.join(__dirname, '../', 'client', 'build', 'index.html')
或
path.join(__dirname, '../client', 'build', 'index.html')
或
path.join(__dirname, '../client/build/index.html')
我的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
目录中。
由于您的 index.js
位于 server
文件夹中,因此绝对路径将始终如 server\client\build\index.html
.
要解决您的问题,您必须更改文件夹结构或手动构建路径。
您必须添加 ../
以将 1 个目录向上移动到您的客户端文件夹所在的位置以构建正确的路径。
path.resolve(__dirname, "../client", "build", "index.html")
path.join(__dirname, '../', 'client', 'build', 'index.html')
或
path.join(__dirname, '../client', 'build', 'index.html')
或
path.join(__dirname, '../client/build/index.html')