Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to lookup view "error" in views directory using handlebars

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:enter image description here

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.

like image 945
Aoi Avatar asked May 27 '14 17:05

Aoi


1 Answers

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'));
like image 114
JuJoDi Avatar answered Sep 24 '22 06:09

JuJoDi