Express 4 js无法在views目录中查找视图

时间:2015-08-11 16:31:16

标签: express handlebars.js

这里有许多类似的问题,类似的问题,但没有一个解决了我的问题。

使用Express 4并出现错误:" 错误:无法查找视图"测试"在views目录" c:\ Users \ App \ views \"    在EventEmitter.render(c:\ Users \ App \ node_modules \ express \ lib \ application.js:579:17)

application.js中的第579行说:

if (!view.path) {
  var dirs = Array.isArray(view.root) && view.root.length > 1
    ? 'directories "' + view.root.slice(0, -1).join('", "') + '" or "' + view.root[view.root.length - 1] + '"'
    : 'directory "' + view.root + '"'
  var err = new Error('Failed to lookup view "' + name + '" in views ' + dirs);
  err.view = view;
  return done(err);
}

这是我的代码:

var express  = require('express'),
exphbs   = require('express-handlebars'),
path     = require('path'),
mongoose = require('mongoose'),
parser   = require('body-parser'),
ranges   = require('./models/ranges.js'),
app      = express();

app.use(parser.json()); // for parsing application/json
app.set('port', process.env.PORT || 3000);
app.use(express.static(__dirname + '/public'));

// View engine - handlebars
app.engine('handlebars', exphbs({ defaultLayout: 'main', extname: '.hbs'   }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'handlebars');
// Index page
app.get('/', function (req, res) {
    res.render('test');
});

App的目录结构:

enter image description here

1 个答案:

答案 0 :(得分:1)

根据the documentation,您的Handlebars设置应如下所示:

CheckedChanged