Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

500 Error: Failed to lookup view #2141

Closed
gahula opened this issue May 26, 2014 · 14 comments
Closed

500 Error: Failed to lookup view #2141

gahula opened this issue May 26, 2014 · 14 comments

Comments

@gahula
Copy link

gahula commented May 26, 2014

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"!

@dougwilson
Copy link
Contributor

Does res.render('errors/404.html', {error: 'Not found'}); work?

@dougwilson
Copy link
Contributor

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.

@gahula
Copy link
Author

gahula commented May 26, 2014

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.

@dougwilson
Copy link
Contributor

What version of express are you using? Can you paste the error here you get when you don't use the stash?

@gahula
Copy link
Author

gahula commented May 26, 2014

Express 4 (I updated this weekend). Currently not in office, will do at my earliest convenience.

@dougwilson
Copy link
Contributor

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.

@gahula
Copy link
Author

gahula commented May 26, 2014

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);

@dougwilson
Copy link
Contributor

Interesting, the error shows that errors directory is included twice: /home/dc/webapps/app/views/errors/errors/index-view.html

@dougwilson
Copy link
Contributor

Can you paste your res.render call that caused the error?

@gahula
Copy link
Author

gahula commented May 26, 2014

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.');
});

@dougwilson
Copy link
Contributor

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.

@gahula
Copy link
Author

gahula commented May 26, 2014

Ok thanks... noted!

@dougwilson
Copy link
Contributor

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.

@gahula
Copy link
Author

gahula commented May 26, 2014

That was the case @dougwilson. Gratitude!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants