16

I have been playing a bit with Node.js. I recently started toying with Express and have been setting up a basic app. I wanted to use Handlebars as my view templating engine, but am hitting a wall - failed to locate view "index.html"

I have index.html in the same directory as app.js and and so I would think the code below would have no problem locating index.html...

I have searched around, but it would seem that comprehensive examples of anything aside from jade are rare... Anyone have experience with this combo?

Thanks in advance!

var express = require('express')
  , app = express.createServer();

app.configure(function(){
    app.set('view engine', 'handlebars');
    app.set("view options", { layout: false }) 
});



app.get('/', function(req, res){

    var data = {
        name: "Ford Prefect",
        home: "a small planet somewhere in the vicinity of Betelgeuse"
    }

    res.render('index.html', data);
});

app.listen(3000);

Update:

I was missing:

app.set('views', __dirname + '/');
app.register('.html', require('handlebars'));

in my config... it would seem that the register of '.html' is quite important as it specifies the handlebars association with .html...

I hope this helps someone...

Because I am a SO noob, I can't answer my own question for 7 hrs, but if anyone needs the complete working example, I can post tomorrow...

Stéphane Bruckert
  • 21,706
  • 14
  • 92
  • 130
mattezell
  • 607
  • 1
  • 6
  • 13
  • 1
    You may consider to name your files .handlebars instead of .html to make it obvious it's not a plain html file. Soon [.hbs](https://github.com/wycats/handlebars.js/issues/174) might work too? – MyGGaN Jun 03 '12 at 13:04

1 Answers1

17

By default, it will look in a folder called views from the directory the script is. If you use a different dir you must specify it.

app.set('views', __dirname + '/views');

Express should also tell you more information about where it's trying to find the view, that should help you know exactly where it's looking.

fent
  • 17,861
  • 15
  • 87
  • 91
  • Exactly. Truth told, this conclusion was arrived at in two steps for me - line 1 and then line 2 in the Update... First I discovered line 1, which lead me to encounter Error: Cannot find module 'html' which, as the story goes, line 2 resolved :) Hope my folly helps someone. – mattezell Jan 13 '12 at 02:55