简体   繁体   English

HEROKU 错误:ENOENT:没有这样的文件或目录,stat '/app/distpublic/index.html'

[英]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.我有一个适合你的解决方案,但你必须先重组你的项目。

  • first move the server.ts into the root directory.首先将 server.ts 移动到根目录。
  • secondly move the public folder that contains your static file into the root directory also and delete the src folder.其次,将包含 static 文件的公用文件夹也移动到根目录并删除 src 文件夹。

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.

相关问题 错误:ENOENT,Heroku 中的 stat '/app/public/views/index.html' - Error: ENOENT, stat '/app/public/views/index.html' in Heroku Heroku:错误:ENOENT:没有这样的文件或目录,打开'/app/build/index.html'吗? - Heroku : Error: ENOENT: no such file or directory, open '/app/build/index.html'? 服务器给出错误:ENOENT:没有这样的文件或目录,stat 'F:\\web development\\calculator\\index.html' - server is giving Error: ENOENT: no such file or directory, stat 'F:\web development\calculator \index.html ' webpack + express + angular 4错误“错误:ENOENT:没有这样的文件或目录,统计信息'C:\\ public \\ index.html' - webpack + express + angular 4 error "Error: ENOENT: no such file or directory, stat 'C:\public\index.html' 错误:ENOENT:没有这样的文件或目录,stat 'C:\Self Project\react-shopper\react-shopper\client\build\index.html' - Error: ENOENT: no such file or directory, stat 'C:\Self Project\react-shopper\react-shopper\client\build\index.html' 错误:ENOENT:没有这样的文件或目录,打开 'dist\browser\index.html' - Error: ENOENT: no such file or directory, open 'dist\browser\index.html' 错误:ENOENT:没有这样的文件或目录,统计 - Error: ENOENT: no such file or directory, stat 错误:Enoent:没有这样的文件或目录,状态为'D:\\ awesome-testindex.html' - Error :Enoent :no such file or directory ,stat 'D:\awesome-testindex.html' 错误:ENOENT:没有那个文件或目录,打开'D:\\d%3A\\VSCodeExt\\fun\\index.html' - Error: ENOENT: no such file or directory, open 'D:\d%3A\VSCodeExt\fun\index.html' 错误:ENOENT:没有这样的文件或目录,stat ... .steampath - Error: ENOENT: no such file or directory, stat ... .steampath
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM