0

I am following the tutorials in O'Reilly's "Web Development with Node & Express" by Ethan Brown.

They use handlebars as the view engine.

Here is my code:

var express = require ('express'),
    handlebars = require('express3-handlebars'),
    app = express();

handlebars.create({ defaultLayout: 'main' });
app.engine('handlebars', handlebars.engine);

The problem I am having is that handlebars.engine is undefined, resulting in a "Callback function expected" error when running the application.

I have tried searching online without any luck.

Is this some legacy syntax with handlebars? My packages have installed fine and I have tried reinstalling them.

Is there a fix/updated code for this?

Asher
  • 393
  • 3
  • 16

1 Answers1

2

You have to get the engine from the object you got from the create()-call!

Like this: var expHbs = require('express-handlebars'); var handlebars = expHbs.create({ defaultLayout: 'layout', extname: '.hbs', helpers: handlebarsHelpers }); app.engine('.hbs', handlebars.engine); app.set('view engine', '.hbs');

Just saying: express3-handlebars got renamed to express-handlebars. You should consider switching.

NeedCoffee
  • 70
  • 2
  • 9
  • Thanks so much. Was really stumped on this. For the purposes of this book, I'm going to use the old one, but will have a play around with the renamed one. Are all the methods and syntax the same? Is it just the name that changed? If so, I probably will make the switch now – Asher May 29 '15 at 13:48
  • 1
    The most things are the same, but some functions, like `res.status(404).end();` are not allowed anymore (`req.sendStatus(404);` is now the correct one) Here is the changelog, since renamed from `express3-handlebars`: https://github.com/ericf/express-handlebars/blob/master/HISTORY.md#100-2014-08-07 – NeedCoffee Jun 01 '15 at 12:24