错误:ENOENT,Heroku 中的 stat '/app/public/views/index.html'

2023-12-22

我在 Heroku 应用程序上提供静态视图时遇到错误。奇怪的是,Heroku 似乎将“app”附加到我的静态文件路径的前面,我不知道为什么。路径应该是“public/views/index.html”。

我最近尝试了 Stack 提出的解决方案,但它似乎不起作用:Node.js,无法打开文件。错误:ENOENT,stat './path/to/file' https://stackoverflow.com/questions/13541948/node-js-cant-open-files-error-enoent-stat-path-to-file

来自我的服务器的获取请求:

app.use(express.static(__dirname + '/public'));

app.get('/', function (req, res) {
  res.sendFile(__dirname + '/public/views/index.html');
});

// profile page
app.get('/profile', function (req, res) {
  // check for current (logged-in) user
  req.currentUser(function (err, user) {
    // show profile if logged-in user
    if (user) {
      res.sendFile(__dirname + '/public/views/profile.html');
    // redirect if no user logged in
    } else {
      res.redirect('/');
    }
  });
});

有谁知道为什么 Heroku 会将“app”附加到我的路径中?

所有路径在本地服务器上都能正常工作。谢谢!


所接受的。这里使用的解决方案PWD代替__dirname是完全错误的。 sendFile 在 Heroku 上的工作方式与在其他地方的工作方式相同:

res.sendFile(path.join(__dirname, 'public', 'views', 'index.html'));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

错误:ENOENT,Heroku 中的 stat '/app/public/views/index.html' 的相关文章

随机推荐