0

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?

user3739383
  • 37
  • 1
  • 8

3 Answers3

0

As you are using hbs as your view system, you should use the .hbs extension for your view files. It fails to lookup the view because its an HTML, and its looking for contact.hbs

I personally dont have much experience with the handlebars library, but I suggest you change the contact.html extension to .hbs and try again

CyborgFish
  • 356
  • 6
  • 13
0

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'}));
Bidhan
  • 10,607
  • 3
  • 39
  • 50
  • notice how the name of the engine and the extension have to be the same... noticed on another thread too... confused me for a while and I can't be the only one (it looks like just an identifier!) – Matthew Bonig Nov 08 '17 at 21:40
0

There is something wrong in the setting views path. Try changing the line

app.set('views', path.join(__dirname, 'views/'));

by

app.set('views', path.join(__dirname, '/views'));

If not solved, show your folder structure.

sam100rav
  • 3,733
  • 4
  • 27
  • 43