Search code examples
javascriptnode.jshandlebars.jsexpress-4

Express 4.0 Failed to Look up view in "/views" directory


I am working with Express 4.0 and Express3-handlebars libraries for NodeJS.

Here is the configuration

app.set('views', path.join(__dirname, 'views/'));
app.engine('hbs', hbs({defaultLayout: 'main', extname: '.hbs'}));
app.set('view engine', 'hbs');

The contact.html is in the views directory

app.get('/', function(req, res) {
 res.render("contact");
});

This is the error i get :

Error: Failed to lookup view "contact" in views directory "/Users/max23/Desktop/Node/views/"

I spent over an hour trying to fix it to no avail. What is Wrong with the code?


Solution

  • You're using Handlebars templating library, so your views should have .hbs extension, not .html. Change contact.html to contact.hbs and do that to all of your templates. Also, change this line

    app.engine('hbs', hbs({defaultLayout: 'main.hbs', extname: '.hbs'}));