javascript - Error: Failed to lookup view in express + jade - Stack Overflow

this is the code I havevar express = require('express');var app = express();app.set('v

this is the code I have

var express = require('express');
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');

exports.makeObservable = function(state, path, cb) {
    app.get(path, function(req, res){
        res.render('observable.jade', state);
        //call callback with new user
        cb(req);
    });

    app.listen('9999'); 
}

Whenever I try to reach the path I receive the following error: Error: Failed to lookup view "observable.jade". I also tried to pass observable without the .jade but it gives the same error. I have both observable.jade and layout.jade in my /views folder.

What am I missing? Thanks!

this is the code I have

var express = require('express');
var app = express();
app.set('view engine', 'jade');
app.set('views', __dirname + '/views');

exports.makeObservable = function(state, path, cb) {
    app.get(path, function(req, res){
        res.render('observable.jade', state);
        //call callback with new user
        cb(req);
    });

    app.listen('9999'); 
}

Whenever I try to reach the path I receive the following error: Error: Failed to lookup view "observable.jade". I also tried to pass observable without the .jade but it gives the same error. I have both observable.jade and layout.jade in my /views folder.

What am I missing? Thanks!

Share Improve this question asked Oct 29, 2012 at 17:01 MasiarMasiar 21.4k31 gold badges100 silver badges140 bronze badges 6
  • 3 Hi. Can't reproduce the error. If you want a quick guess, maybe express is doing the lookup in the directory __dirname + 'views' which is wherever the script is running, for example, if the script lives in /herman/code/myapp/scripts, it will look in /herman/code/myapp/scripts/views, instead of /herman/code/myapp/views... Send more data, please. – hhh Commented Oct 29, 2012 at 17:49
  • 2 To debug this go to line 493 of express/lib/application.js and add console.log(view.path); This will log what path your app is trying to hit. – Pickels Commented Oct 29, 2012 at 21:23
  • Thanks to both. It's true that I had my /view folder outside the running directory of the script, but also putting it in the right directory didn't solve the problem. I tried with the console.log() but it printed out undefined. What can I do? – Masiar Commented Oct 30, 2012 at 9:39
  • By printing view instead that view.path I could notice that the script was indeed run from a totally different folder. I managed to solve the problem in the end. Thanks. :) – Masiar Commented Oct 30, 2012 at 9:55
  • Somebody should take my answer + herman's and make it into a real answer cause this could be helpful for others. – Pickels Commented Oct 30, 2012 at 18:47
 |  Show 1 more ment

2 Answers 2

Reset to default 2

I finally solved the problem thanks to the suggestions in the ments. First of all I checked if the directory from which the script was executing contained the directory /views where I put the .jade files. I couldn't solve the problem at first since I couldn't figure from which this script was executed, so I put a console.log(view) at line 493 in /express/lib/application.js. It printed out the view object that contained the root from which the script was executed, thus I moved the /views folder there and everything worked.

FWIW: I have my views in an adjacent directory (ie root/server, root/client), and was having the same issues described here. The one thing I did differently than the OP, was to prepend a '/' in my app.set when climbing out of the server directory; ie:

this: app.set('views', __dirname + '/../client/');

instead of: app.set('views', __dirname + '../client/');

HTH

(Thanks a TON to the OP and Pickels!)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745488565a4629881.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信