【问题标题】:HEROKU Error: ENOENT: no such file or directory, stat '/app/distpublic/index.html'HEROKU 错误:ENOENT:没有这样的文件或目录,stat '/app/distpublic/index.html'
【发布时间】:2021-03-04 00:29:15
【问题描述】:

我知道网站上有很多这样的问题,但我已经在这个网站上尽我所能,我能在 heroku 上工作的最好方法是在浏览器上显示一个文本,我一直在努力解决这个问题现在代码2天,没有成功,下面是我的目录布局。 image of my directory structure

下面是我的代码

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"));

当我在我的机器上运行此代码时,它运行良好,实际上有近 5 种方法可以更改代码,它仍然可以在我的机器上运行,但不能在 heroku 上运行,我让 horoku 工作的最好方法是只是为了显示一个文本。我需要如何使这项工作,现在已经超过 2 天了,我什至不明白错误消息中找不到目录

【问题讨论】:

    标签: javascript node.js express heroku


    【解决方案1】:

    我有一个适合你的解决方案,但你必须先重组你的项目。

    • 首先将 server.ts 移至根目录。
    • 其次将包含您的静态文件的公用文件夹移动到 根目录也删除 src 文件夹。

    以下是解决方案。

    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"));
    

    你的端口号也是一个字符串,以后可能会成为问题。

    【讨论】:

      猜你喜欢
      • 2019-05-08
      • 2017-01-24
      • 2021-09-22
      • 1970-01-01
      • 2021-04-26
      • 2021-04-04
      • 1970-01-01
      • 2020-06-03
      • 2019-08-22
      相关资源
      最近更新 更多