首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Express中使用车把?

如何在Express中使用车把?
EN

Stack Overflow用户
提问于 2018-08-06 01:39:51
回答 1查看 1.6K关注 0票数 0

这个问题其实并不难理解,我不知道如何在Express中实现handlebars。

这就是我已经编写的代码:

var express = require('express');
var app = express();

app.get('/', function (req, res, next) {
        return res.render('index');
});

现在我的问题是,如何将handlebars设置为express的应用程序引擎?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-06 01:39:51

这是我目前使用和学习的代码。我在每个重要的行后面都添加了注释,以便您更好地理解它!

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)
});
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51696768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档