Reputation: 355
Many similar questions on here with a similar issue but none of them have resolved my issue.
Using Express 4 with the error: "Error: Failed to lookup view "test" in views directory "c:\Users\App\views\" at EventEmitter.render (c:\Users\App\node_modules\express\lib\application.js:579:17)
Line 579 in the application.js says:
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);
}
Here is my code:
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');
});
Directory structure of App:
Upvotes: 1
Views: 7093
Reputation: 203329
As per the documentation, your Handlebars setup should look like this:
app.engine('.hbs', exphbs({ defaultLayout: 'main', extname: '.hbs' }));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', '.hbs');
Upvotes: 2