在heroku中部署我的应用程序时获取html时出错,HTML >>

问题描述 投票:0回答:1

这是我在express node.js中的index.js文件,该应用程序运行良好,直到将其部署到Heroku上为止(在生产环境中,它抛出了该错误,就像服务器找不到我的index.html。

*-src--- index.html--- index.jsenter image description here*

app.use(express.urlencoded({ extended: true }))

function renderHTML(path, response) {
    fs.readFile(path, null, function(error, data) {
        if (error) {
            response.writeHead(404);
            response.write('File not found!');
        } else {
            response.write(data);
        }
        response.end();
    });
}

app.get('/', function (req, res) {
  renderHTML('./index.html', res);
});

这是Express node.js中的index.js文件,该应用程序可以正常运行,直到将其部署到Heroku上为止(在生产环境中。它抛出该错误,就像服务器找不到我的index.html一样。) * -src ---...

javascript node.js express heroku
1个回答
0
投票

您需要使用绝对文件路径:path.resolve(__dirname, './index.html')。例如:

© www.soinside.com 2019 - 2024. All rights reserved.