nodejs handlebars: 'TypeError: this.engine is not a function'
nodejs handlebars: 'TypeError: this.engine is not a function'
我尝试在 Express 中使用 handlebars 模板。我收到此错误消息:
TypeError: this.engine is not a function
at View.render (/home/ubuntu/workspace/node_modules/express/lib/view.js:128:8) ...
使用此代码:
'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 + '...');
});
当我用 sendFile() 替换 render() 函数时工作正常。我知道 and Express js render error this.engine is not a function and ,但他们对我没有帮助。
可能是什么问题?
由于您的车把模板文件保存为 .html
,您应该将引擎注册为 'html'
而不是 'handlebars'
:
app.engine('html', expressHandlebars({defaultLayout: 'layout', extname: '.html', layoutsDir: 'public/'}));
app.set('view engine', 'html');
app.set('views', 'public/');
// ...
res.render('single-poll', ...);
你的项目目录应该是这样的(我希望如此):
├── app.js
└── public
├── layout.html
└── single-poll.html
找到您需要更改的所有内容后,我真的建议您坐下来 read the ******* manual,因为我建议的一切都在那里。
感谢@Patrick Roberts 关于阅读手册的评论,我重写了它,它是这样工作的:
'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 + '...');
});
我认为最主要的是将文件结构更改为:
├── app.js
└── views
├── single-poll.handlebars
└── layouts
└── main.handlebars
我尝试在 Express 中使用 handlebars 模板。我收到此错误消息:
TypeError: this.engine is not a function at View.render (/home/ubuntu/workspace/node_modules/express/lib/view.js:128:8) ...
使用此代码:
'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 + '...');
});
当我用 sendFile() 替换 render() 函数时工作正常。我知道
可能是什么问题?
由于您的车把模板文件保存为 .html
,您应该将引擎注册为 'html'
而不是 'handlebars'
:
app.engine('html', expressHandlebars({defaultLayout: 'layout', extname: '.html', layoutsDir: 'public/'}));
app.set('view engine', 'html');
app.set('views', 'public/');
// ...
res.render('single-poll', ...);
你的项目目录应该是这样的(我希望如此):
├── app.js
└── public
├── layout.html
└── single-poll.html
找到您需要更改的所有内容后,我真的建议您坐下来 read the ******* manual,因为我建议的一切都在那里。
感谢@Patrick Roberts 关于阅读手册的评论,我重写了它,它是这样工作的:
'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 + '...');
});
我认为最主要的是将文件结构更改为:
├── app.js
└── views
├── single-poll.handlebars
└── layouts
└── main.handlebars