Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

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

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

Problem

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");
Problem courtesy of: Charles

Solution

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);
});
Solution courtesy of: thtsigma

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

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

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×