【问题标题】:How to use .html file extensions for handlebars in express?如何在 express 中使用 .html 文件扩展名作为车把?
【发布时间】:2014-03-09 03:27:18
【问题描述】:
所以我想知道如何使用 .html 扩展名而不是 .handlebars 或 .hbs 扩展名。我这样做是为了可以使用常规 html 进行开发,这样我的前端开发人员就可以在他们的 IDE 中无缝编辑文件,而无需任何额外的配置。此外,它将有助于更快地将 html 模板安装到我们的 express 应用程序中。
【问题讨论】:
标签:
node.js
express
handlebars.js
template-engine
file-extension
【解决方案1】:
所以我可以通过更改 app.js 文件中的三件事来做到这一点,我希望这对每个人都有帮助!
var express = require('express'),
exphbr = require('express3-handlebars'), // "express3-handlebars"
helpers = require('./lib/helpers'),
app = express(),
handlebars;
// Create `ExpressHandlebars` instance with a default layout.
handlebars = exphbr.create({
defaultLayout: 'main',
helpers : helpers,
extname : '.html', //set extension to .html so handlebars knows what to look for
// Uses multiple partials dirs, templates in "shared/templates/" are shared
// with the client-side of the app (see below).
partialsDir: [
'views/shared/',
'views/partials/'
]
});
// Register `hbs` as our view engine using its bound `engine()` function.
// Set html in app.engine and app.set so express knows what extension to look for.
app.engine('html', handlebars.engine);
app.set('view engine', 'html');
// Seperate route.js file
require("./routes")(app, express);
app.listen(3000);