Reputation: 1045
I am new with express and handlebars. I wanted to use handlebars as a template engine on my express.js app but then I keep on receiving this kind of error:
which was get generated by this code
var express = require('express');
var path = require('path');
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbr = require('express3-handlebars'); // "express3-handlebars"
var routes = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views/'));
//app.set('view engine', 'jade');
app.engine('handlebars', exphbr({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
/// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
module.exports = app;
what else am I missing or am I doing it right? Please help, I have been stuck for 1 day.
Upvotes: 12
Views: 30858
Reputation: 35
If the view engine is not set to jade/pug you have to use the extension right here:
app.use(function(err, req, res, next) {
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error.pug'); <<<<<HERE!
});
Upvotes: 2
Reputation: 39
I had this issue when I upgraded the server. All I had to do is run this
npm update
Upvotes: 3
Reputation: 121
You should have a View within your application currently named error.jade. Because you set up your application to use Handlebars the view engine is attempting to look for error.handlebars. You should put modify the file and that will do it.
Upvotes: 11
Reputation: 14975
The issue is the following line
app.set('views', path.join(__dirname, 'views/'));
Express is looking for a file called error.jade
in the folder views
. If the file exists, possibly try removing the extra /
from views/
.
I just ran into a similar problem because I moved the views
folder into another folder called app_server
so the solution was to replace the line with
app.set('views', path.join(__dirname, '/app_server/views'));
Upvotes: 13