这是我在express node.js中的index.js文件,该应用程序运行良好,直到将其部署到Heroku上为止(在生产环境中,它抛出了该错误,就像服务器找不到我的index.html。
*-src--- index.html--- index.js*
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 ---...
您需要使用绝对文件路径:path.resolve(__dirname, './index.html')
。例如: