Error handeling when file is not found in nodeJS and Express framework
I want to display multiple html/jade files using node/express.
I'm trying to create a basic rule for handeling non-existing files.
If I would write this code I will not handle error responses but it will
work:
app.get('/art/:project', function(req, res){
var project = req.param('project');
res.render('art/' + project);
});
if I'm writing this code it will handle error correctly but for some
reason will not display the page when the file DO exist.
app.get('/art/:project', function(req, res){
var project = req.param('project');
res.render('art/' + project, function(err, html) {
if (err) {
res.redirect('/');
}
});
});
What am I missing?
I've found out that this will work but it seems extremely un-efficient:
res.render('art/' + project, function(err, html) {
console.log(err, html);
if (err) {
res.redirect('/');
} else {
res.render('art/' + project);
}
});
No comments:
Post a Comment