[英]Express hbs does not work
I know this post is a kinda dupplicate, but I can't find out where I'm wrong. 我知道这篇文章有点重复,但是我找不到我错了。
/// my app.js // ///我的app.js //
var express = require('express'), app = express(); app.use(express.static('public')); app.use(express.static('views')); // app.engine('html', require('hbs').__express); app.set('views', 'views'); app.set('view engine', 'html'); app.get('/', function(req, res) { // res.sendFile(path.join(__dirname + '/index.html')); //res.sendFile('/index.html'); res.render('index',{title :"page index"}); }); app.listen(3000);
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Index</title> </head> <body> <h1>{{title}}</h1> </body> </html>
Any help ? 有什么帮助吗? thanks !
谢谢 !
You should use .hbs as the extension. 您应该使用.hbs作为扩展名。
Try to rename your /views/index.html
to /views/index.hbs
尝试将
/views/index.html
重命名为/views/index.hbs
and change it in the code as follows 并在代码中进行如下更改
app.set('view engine', 'hbs');
Apparently hbs wants .hbs to be used. 显然,hbs希望使用.hbs。
Tip: hbs hbs does not look so good to me, you can take a look at swig: http://paularmstrong.github.io/swig/docs 提示:hbs hbs对我不太好,您可以看看swig: http : //paularmstrong.github.io/swig/docs
Try the below code 试试下面的代码
app.set('view engine', 'html');
app.set('view engine','html');
app.engine('html', require('hbs').__express);app.engine('html',require('hbs').__ express);
Instead of the this 而不是这个
app.engine('html', require('hbs').__express);
app.engine('html',require('hbs').__ express);
app.set('views', 'views');app.set('views','views');
app.set('view engine', 'html');app.set('view engine','html');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.