无法使用Express + jade + angular查找视图

时间:2014-07-07 17:04:08

标签: node.js angularjs express pug

像我之前的许多人一样,我收到以下错误:

Failed to lookup view "/partials/test" in views directory    "/home/aveline/git/multivision/server/views"

由于我使用Jade,我的模板通过app.get呈现(这是我的server.js文件):

var express = require('express'), 
stylus = require('stylus'),
logger = require('morgan'),
bodyParser = require('body-parser');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

function compile(str, path){
   return stylus(str).set('filename', path);
}

app.set('views', __dirname + '/server/views');
app.set('view engine','jade');
app.use(logger('dev'));
app.use(bodyParser());
app.use(stylus.middleware(
    {
        src: __dirname + '/public',
        compile: compile
    }
));
app.use(express.static(__dirname + '/public'));

app.get('/partials/:partialPath',function(req, res){
    res.render('/partials/' + req.params.partialPath);
});
app.get('*', function(req, res){
    res.render('index');
});

var port = 3030;
app.listen(port);
console.log("Listening on port " + port);

问题是,这个模板“test.jade”实际上就在那个目录中。所以当我去localhost:3030(我听的端口)时,我得到这个错误(全栈):

Error: Failed to lookup view "/partials/test" in views directory "/home/aveline/git/multivision/server/views"
at Function.app.render (/home/aveline/git/multivision/node_modules/express/lib/application.js:485:17)
at ServerResponse.res.render (/home/aveline/git/multivision/node_modules/express/lib/response.js:794:7)
at Object.port [as handle] (/home/aveline/git/multivision/server.js:27:6)
at next_layer (/home/aveline/git/multivision/node_modules/express/lib/router/route.js:113:13)
at Route.dispatch (/home/aveline/git/multivision/node_modules/express/lib/router/route.js:117:5)
at /home/aveline/git/multivision/node_modules/express/lib/router/index.js:222:24
at param (/home/aveline/git/multivision/node_modules/express/lib/router/index.js:307:14)
at param (/home/aveline/git/multivision/node_modules/express/lib/router/index.js:323:14)
at Function.proto.process_params (/home/aveline/git/multivision/node_modules/express/lib/router/index.js:367:3)
at next (/home/aveline/git/multivision/node_modules/express/lib/router/index.js:216:19)

有人可以帮我吗?我已经在其他线程上尝试了其他一些解决方案但到目前为止都没有。

如果您需要更多代码示例,请在评论中说明。

编辑:我找到了解决方案,请看下面的答案。

1 个答案:

答案 0 :(得分:2)

似乎要解决这个问题,我必须删除res.render()位中的/之前的部分。它搜索的完整路径是/ home / aveline / git / multivision / server / views // partials / test

解决方案:

app.get('/partials/:partialPath',function(req, res){
    res.render('partials/' + req.params.partialPath);
});