2015-07-20 84 views
2

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

我最近尝试从堆栈这个提议的解决方案,但它似乎没有工作:Node.js, can't open files. Error: ENOENT, stat './path/to/file'

从我的服务器的GET请求:

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追加“应用程序”我的路径?

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

回答

1

这是因为Heroku内部的全局__dirname变量设置为/app。使用process.env.PWD而不是__dirname

0

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

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