Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'

Trying to use handlebars with nodejs and I get this error "TypeError: Object function app(req, res){ app.handle(req, res); } has no method 'register'". Below is the code for the nodejs. The same code seems to have worked for other people since it's pretty much a copy paste of NodeJS + Express + Handlebars - failed to locate view "index.html". P.S. I am new to nodejs and trying to get a feel for it and am already used to handlebars.

//Load Modules
var express = require('express');
var handlebars = require('handlebars');

var app = express();

// Configuration
app.configure( function() {
    app.register('.html', handlebars);
    app.set('views', __dirname + '/');
    app.set('view engine', 'handlebars');
    app.set("view options", { layout: false });    
});

// Routes
app.get('/:first/:last', function(req, res) {
    var data = {title:req.param.first + " " + req.param.last};
    res.render("template/profilecard.html", data);
});

app.listen(3000);

console.log("NodeJS Server Started");
like image 971
Charles Avatar asked Jan 15 '13 18:01

Charles


1 Answers

Express 3.0 changed the app.register to app.engine. Migrating to Express.js 3.0

Some template engines do not follow this convention, the consolidate.js library was created to map all of node's popular template engines to follow this convention, thus allowing them to work seemlessly within Express.

npm install consolidate

Try the following:

var engines = require('consolidate');

app.configure( function() {

    app.set('views', __dirname + '/');
    app.set('view engine', 'handlebars');
    app.set("view options", { layout: false }); 
    app.engine('.html', engines.handlebars);
});
like image 97
thtsigma Avatar answered Sep 21 '22 19:09

thtsigma