500 Error: Failed to lookup view
There is a sub directory in views called errors and within that errors file there is '404.html' and '500.html'. In my app I have which is where the error happens (This is the same thing I had with the previous SWIG which worked fine)....
app.use(function(req, res, next){
res.status(404);
// respond with html page
if(req.accepts('html')) {
res.render('/errors/404.html', {error: 'Not found'});
return;
};
// respond with json
if(req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text. send()
res.type('txt').send('Not found');
});
}
500 Error: Failed to lookup view "/errors/500.html" in views directory "/home/dc/webapps/app/views"
It isn't going into the subdirectory "errors"!
Does res.render('errors/404.html', {error: 'Not found'}); work?
Edit: express does the lookup, but you provided an absolute file system path with your leading slash. Remove the leading slash and it should work.
I did it without and with the forward slash the same 500 Error exist. I use swig. I used it with and without consolidate. Same Error exist.
What version of express are you using? Can you paste the error here you get when you don't use the stash?
Express 4 (I updated this weekend). Currently not in office, will do at my earliest convenience.
Currently not in office, will do at my earliest convenience.
OK. As far as I can tell from the error in your original post, not including the leading stash will definitely work, assuming a file exists at /home/dc/webapps/app/views/errors/404.html. Please report back when you've tried it and if it still doesn't work, paste me the error you get when not using the forward slash.
Using it without the forward slash:
500 Error: ENOENT, no such file or directory '/home/dc/webapps/app/views/errors/errors/index-view.html'
at Object.fs.openSync (fs.js:427:18)
at Object.fs.readFileSync (fs.js:284:15)
at Object.ret.load (/home/dc/webapps/app/node_modules/swig/lib/loaders/filesystem.js:55:15)
at parseFile (/home/dc/webapps/app/node_modules/swig/lib/swig.js:369:31)
at getParents (/home/dc/webapps/app/node_modules/swig/lib/swig.js:436:45)
at Object.precompile (/home/dc/webapps/app/node_modules/swig/lib/swig.js:480:17)
at Object.compile (/home/dc/webapps/app/node_modules/swig/lib/swig.js:584:16)
at Function.exports.swig.render (/home/dc/webapps/app/node_modules/consolidate/lib/consolidate.js:246:56)
at /home/dc/webapps/app/node_modules/consolidate/lib/consolidate.js:146:25
at /home/dcwebapps/app/node_modules/consolidate/lib/consolidate.js:99:5
Config
app.engine('html', consolidate.swig);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.set('view cache', false);
Interesting, the error shows that errors directory is included twice: /home/dc/webapps/app/views/errors/errors/index-view.html
Can you paste your res.render call that caused the error?
It is actually trying to print a 500 Error when i comment out the if(req.accepts('html')) {
it prints the text version
"Something is broken on our end, email us if this issue persist."
however the console.log(err) prints
{ [Error: ENOENT, no such file or directory '/home/a9designs/webapps/gahoola_desktop/views/errors/errors/index-view.html']
errno: 34,
code: 'ENOENT',
path: '/home/a9designs/webapps/gahoola_desktop/views/errors/errors/index-view.html',
syscall: 'open' }
For some reason the Express is adamant in looking in the errors sub-directory
app.use(function(err, req, res, next){
console.log(err);
// we may use properties of the error object
// here and next(err) appropriately, or if
// we possibly recovered from the error, simply next().
res.status(err.status || 500);
// respond with html page
/*if(req.accepts('html')) {
res.render('errors/500.html', {error: 'Something is broken on our end, email us if this issue persist.'});
return;
}*/
// default to plain-text. send()
res.type('txt').send('Something is broken on our end, email us if this issue persist.');
});
Yea, this error is not in express, I'm sorry to tell you. Look at the error stack trace you posted above: none of the files are in express.
Only the error Failed to lookup view "{name}" in views directory "{root}" comes from express. Once you get past that error (which you did by removing the leading slash), express just passes the path to your view engine. You will need to open an issue at consolidate or swig, not here.
Ok thanks... noted!
What is the content of your errors/500.html file? It seems like this error may be caused with the content of your file doing something, like requiring another file inside it.
That was the case @dougwilson. Gratitude!