【发布时间】:2020-08-20 12:25:35
【问题描述】:
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
const port = process.env.PORT || 3002;
const cors = require("cors");
const path=require("path");
const routing = require("./routing/route");
require('dotenv').config();
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use("/",routing);
app.use(function (err, req, res, next) {
console.log("hii")
res.status(500).send({ message: err.message });
});
if(process.env.NODE_ENV ==="production"){
app.use(express.static("client/build"));
app.get("*",(req,res)=>{
res.sendFile(path.resolve((__dirname,"client","build","index.html")));
});
}
app.listen(port, () => {
console.log(` Server is started at http://localhost:${port}`);
});
我注意到我的后端在日志中运行我可以从我的代码中看到 //console.log( Server is started at http://localhost:${port})// 这部分。
但我的前端可以正常工作。
在日志文件中我可以看到这些错误。
TypeError: path must be absolute or指定root to res.sendFile 错误:ENOENT:没有这样的文件或目录,stat '/app/index.html'
我猜这两个错误都与同一个路径有关。enter image description here
我也附上了我的文件夹结构
【问题讨论】:
-
我已经尝试过 path.join,因为我在其他地方看到过它,但在这种情况下我遇到了内部服务器错误。
标签: javascript node.js express heroku