快递未能查看视图

我正在尝试创build一个SPA应用程序,但是当我启动我的应用程序时,会发生一个导致崩溃的无限循环。 我正在使用ExpressJS 4.3.0

应用架构:

上市

–partials

—- index.jade

server.js

我的快递代码:

var app = express(); var port = process.env.PORT || 8080; app.set('views', __dirname + '/public'); app.set('view engine', 'jade'); app.use(express.static(__dirname + '/public')); app.get('*', function (req, res) { res.render('partials/index'); }); app.get('/', function (req, res) { res.render('partials/index'); }); app.get('/partials/:name', function (req, res) { res.render('/partials/' + req.params.name); }); app.listen(port); console.log('Server is running on port: ' + port); 

如果我使用

 res.render('/partials/index'); 

我收到一条消息:

 Error: Failed to lookup view "/partials/index" in views directory 

它在快速查看查找中因为查看function

 if (!utils.isAbsolute(path)) path = join(this.root, path); 

这使得它假设'/ partial / index'已经是绝对path,并且没有根path的前缀。

也移动

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

否则它将始终服务于索引视图。

看起来你不应该在render()视图path中有一个前面的/ 。 只要使用'partials/' + req.params.name

在一个相关的说明,你确定你想让你的实际视图文件公开吗? 通常它们存储在公共静态目录之外(例如./public包含静态资源,./ ./partials包含视图)。 也是这样,你不必保留前缀视图path与partials/