Question

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...

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top