最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - "Failed to lookup view" in basic Express.js application - Stack Overflow

programmeradmin2浏览0评论

I'm trying to learn Express.js, so I created simple Express app. The app.js looks as follows:

var express = require('express'); 
var app = express();

app.configure(function(){
    app.set('view engine', 'jade'); 
    app.set('views', __dirname);
});

app.get('/', function(req, res){ 
    res.render("index");
});

app.listen(8080);

My folders structures looks following:

However, when I point my browser to http://localhost:8080 I'm getting the following output:

Error: Failed to lookup view "index"
at Function.app.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/application.js:492:17)
at ServerResponse.res.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/response.js:721:7)
at /Users/kuba/Documents/ws_javascript/express_spike/app.js:10:6
at callbacks (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:161:37)
at param (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:33:10)
at next (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/node_modules/connect/lib/proto.js:199:15)
at Object.expressInit [as handle] (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/middleware.js:31:5)

I've checked and the permissions for the files are ok. Any ideas why express cannot find my index.jade file?

I'm trying to learn Express.js, so I created simple Express app. The app.js looks as follows:

var express = require('express'); 
var app = express();

app.configure(function(){
    app.set('view engine', 'jade'); 
    app.set('views', __dirname);
});

app.get('/', function(req, res){ 
    res.render("index");
});

app.listen(8080);

My folders structures looks following:

However, when I point my browser to http://localhost:8080 I'm getting the following output:

Error: Failed to lookup view "index"
at Function.app.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/application.js:492:17)
at ServerResponse.res.render (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/response.js:721:7)
at /Users/kuba/Documents/ws_javascript/express_spike/app.js:10:6
at callbacks (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:161:37)
at param (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:135:11)
at pass (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:142:5)
at Router._dispatch (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:170:5)
at Object.router (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/router/index.js:33:10)
at next (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/node_modules/connect/lib/proto.js:199:15)
at Object.expressInit [as handle] (/Users/kuba/Documents/ws_javascript/express_spike/node_modules/express/lib/middleware.js:31:5)

I've checked and the permissions for the files are ok. Any ideas why express cannot find my index.jade file?

Share Improve this question asked Feb 23, 2014 at 15:08 JakubJakub 3,1298 gold badges46 silver badges63 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

Try this code:

var express = require('express');
var app = express();

app.use(express.logger('dev'))

app.set('views', __dirname+'/views');
app.set('view engine', 'jade');

app.get('/', function(req, res){
    res.render("index");
});

app.listen(8080);

Then place your index.jade inside a folder called views.

And make sure you have jade inside your node_modules folder

发布评论

评论列表(0)

  1. 暂无评论