1
votes

I try to use handlebars template in Express. I get this Error-Message:

TypeError: this.engine is not a function at View.render (/home/ubuntu/workspace/node_modules/express/lib/view.js:128:8) ...

with this code:

'use strict';

var express = require('express');
var expressHandlebars  = require('express-handlebars');

var app = express();

app.engine('handlebars', expressHandlebars({defaultLayout: 'layout'}));
app.set('view engine', 'html');
app.use(express.static('views'));

app.route('/single/:id')
    .get(function (req, res) {
        res.render(process.cwd() + `/public/single-poll`, {
            id: req.params.id
        });
    });

app.listen(process.env.PORT,  function () {
    console.log('Node.js listening on port ' + process.env.PORT + '...');
});

When I replace the render() function with sendFile() is works fine. I'm aware of Express js render : TypeError: this.engine is not a function and Express js render error this.engine is not a function and TypeError: this.engine is not a function when trying to use Moustache in Express JS, but they didn't help for me.

What can be the problem?

2

2 Answers

2
votes

Since your handlebars template files are saved as .html, you should register the engine as 'html' and not 'handlebars':

app.engine('html', expressHandlebars({defaultLayout: 'layout', extname: '.html', layoutsDir: 'public/'}));
app.set('view engine', 'html');
app.set('views', 'public/');

// ...
res.render('single-poll', ...);

And your project directory should look like this (I hope):

├── app.js
└── public
    ├── layout.html
    └── single-poll.html

After finding all the things you needed to change, I really recommend you sit down and read the ******* manual, because everything I've suggested is right out of there.

0
votes

Thanks to the comment from @Patrick Roberts concerning reading manuals, I rewrote it and this is how it works:

'use strict';

var express = require('express');
var exphbs  = require('express-handlebars');

var app = express();

app.engine('handlebars', exphbs({defaultLayout: 'main'}));
app.set('view engine', 'handlebars');

app.route('/single/:id')
    .get(function (req, res) {
        res.render('single-poll', {
            id: req.params.id
        });
    });

app.listen(process.env.PORT,  function () {
    console.log('Node.js listening on port ' + process.env.PORT + '...');
});

The main thing was, I think, changing the file structure to this:

├── app.js
    └── views
        ├── single-poll.handlebars
        └── layouts
            └── main.handlebars