错误:ENOENT,stat'/app/public/views/index.html'在Heroku中 [英] Error: ENOENT, stat '/app/public/views/index.html' in Heroku
问题描述
我在Heroku应用程序中提供静态视图时出错。奇怪的是,Heroku似乎在我的静态文件路径的前面添加了app,我不知道为什么。路径应该是public / views / index.html。
我最近尝试了Stack提出的这个解决方案,但它似乎并不奏效:
从我的服务器获取请求:
app.use(express.static(__ dirname +'/ public')); (b,b,b,b,b,b, );
$ b $ // profile page
app.get('/ profile',function(req,res){
//检查当前(登录)用户
req.currentUser(function(err,user){
//如果登录用户显示配置文件
if(user){
res.sendFile(__ dirname +'/ public / views / );
//如果没有用户登录,则重定向
} else {
res.redirect('/');
}
});
});
有人知道为什么Heroku会将app附加到我的路径中吗?
所有路径在本地服务器上正常工作。感谢!
这是因为Heroku中的全局变量 __ dirname
设置为 / app
。使用 process.env.PWD
而不是 __ dirname
。
I'm having an error serving static views on a Heroku app. Strangely, Heroku seems to append "app" to the front of my static file paths, and I'm not sure why. The path should be "public/views/index.html."
I recently tried this proposed solution from Stack, but it didn't seem to work: Node.js, can't open files. Error: ENOENT, stat './path/to/file'
The get requests from my server:
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('/');
}
});
});
Does anyone have any idea why Heroku would append "app" to my paths?
All the paths work correctly on a local server. Thanks!
This is because the global __dirname
variable inside Heroku is set to /app
. Use process.env.PWD
instead of __dirname
.
这篇关于错误:ENOENT,stat'/app/public/views/index.html'在Heroku中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!