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

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

我最近尝试了这个来自Stack的build议解决scheme,但它似乎没有工作: Node.js,无法打开文件。 错误: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将追加“应用程序”我的path?

所有path在本地服务器上正常工作。 谢谢!

这是因为Heroku中的全局__dirnamevariables被设置为/app 。 使用process.env.PWD而不是__dirname

被接受。 这里使用PWD而不是__dirname解决scheme是错误的。 sendFile在Heroku上的工作方式与其他地方一样:

 res.sendFile(path.join(__dirname, 'public', 'views', 'index.html'));