在基本的Express.js应用程序中“无法查找视图”

时间:2014-02-23 15:08:04

标签: javascript node.js express pug

我正在尝试学习Express.js,所以我创建了简单的Express应用程序。 app.js看起来如下:

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

我的文件夹结构如下: enter image description here

然而,当我将浏览器指向http://localhost:8080时,我得到以下输出:

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)

我已经检查过了,文件的权限还可以。 任何想法为什么快递都找不到我的index.jade文件?

1 个答案:

答案 0 :(得分:7)

试试这段代码:

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

然后将index.jade放在名为views的文件夹中。

确保jade文件夹中有node_modules