javascript - 如何在 Express 中使用 Handlebars?
问题描述
这个问题其实不难理解,不知道在Express中如何实现handlebars。
这就是我已经编码的内容:
var express = require('express');
var app = express();
app.get('/', function (req, res, next) {
return res.render('index');
});
现在是我的问题,如何将车把设置为快递的应用引擎?
解决方案
这是我目前使用和学习的代码。我在每条重要的行后面添加了一个注释,以便您很好地理解它!
var express = require('express');
var app = express();
var handlebars = require('express-handlebars');
app.engine('handlebars', handlebars({ // Here we define what format you will use (That means what's at the end of each file, for example test.handlebars or test.hbs)
defaultLayout: 'main', // That's the name of your template file. In my case it's main.handlebars
layoutsDir: __dirname + '/views/layouts/' // That's the directory where the template file is
}));
app.set('views', path.join(__dirname, 'views')); // Here you give express the information that it has to look at all files that are in the path /views
app.set('view engine', 'handlebars'); // Here you say express that you are using handlebars to build your website
app.get('/home', function (req, res, next) { // That's a simple GET request (This GET request gets triggered when you enter http://localhost/home for example)
return res.render('index'); // Here we render the index.handlebars file (that is in the /views folder)
});
推荐阅读
- c - 为什么不同的编译器对相同的代码有不同的行为?
- ios - iOS 如何更快地从 PHAsset 导出视频?
- teensy - Teensy 作为串行设备
- python - keras lstm 的输出形状错误
- javascript - 可以在节点js中使用ajax将从客户端接收到的数据发送到php吗?
- reactjs - 如何在 create-react-app 中使用 Jest“项目”配置?
- python - 将 R 示例函数与 rpy2 一起使用时出现意外行为?
- algorithm - How to split range in groups?
- javascript - 使用媒体查询仅包含桌面上的 JS 文件
- html - 在旧项目中使用 vue 导入