[英]HEROKU Error: ENOENT: no such file or directory, stat '/app/distpublic/index.html'
I know there are alot of questions like this on the site but I ihave tried everything i could on this site, the best I could get to work on heroku is to display a text on the browser, I have been struggling with this code for 2 days now, no success, below is my directory layout.我知道网站上有很多这样的问题,但我已经在这个网站上尽我所能,我能在 heroku 上工作的最好方法是在浏览器上显示一个文本,我一直在努力使用这段代码 2几天了,没有成功,下面是我的目录布局。 image of my directory structure我的目录结构的图像
so below are my code所以下面是我的代码
app.use(express.static(__dirname + "/public/"));
app.get("/", (req, res, next) => {
res.sendFile(path.join(__dirname + "public", 'index.html'));
//res.send('Testing one two');
});
const port = process.env.PORT || '5000';
app.listen(port, () => console.log("Server running on port 5000"));
when I run this code on my machine it works well, in fact there are close to 5 ways and could change the code and it still works on my machine, but not on heroku, the best I have gotten horoku to work is just to display a text.当我在我的机器上运行此代码时,它运行良好,实际上有近 5 种方法可以更改代码,它仍然可以在我的机器上运行,但不能在 heroku 上运行,我让 horoku 工作的最好方法就是显示一段文字。 i need on how to make this work, it been more than 2days now, I don't even understand the directory not found in the error msg我需要如何使这项工作,现在已经超过 2 天了,我什至不明白错误消息中找不到目录
I have a solution which will work for you, but you have to restructure your project first.我有一个适合你的解决方案,但你必须先重组你的项目。
Below are the solutions.以下是解决方案。
app.use(express.static(__dirname + "/public"));
app.get("/", (req, res, next) => {
res.sendFile( path.resolve( __dirname, 'public/index.html' ) );
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log("Server running on port 5000"));
also your port number is a string, that might become a problem later.您的端口号也是一个字符串,以后可能会成为问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.