如何在express中使用.html文件扩展名为handlebars?

所以我想知道如何使用.html扩展名而不是.handlebars或.hbs扩展名。 我这样做,所以我可以开发使用普通的HTML,这样我的前端开发人员可以在他们的IDE无缝编辑文件没有任何额外的configuration。 此外,它还将帮助我们快速安装HTML模板。

所以我可以通过在我的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); 

同意JemiloII

  1. 在创buildexpr-HBS实例( exphbr.create() )时根据https://www.npmjs.org/package/express3-handlebars#-extname-handlebars-在configuration文件中的extname: '.myext'
  2. 绑定expr-HBS引擎到扩展名: app.engine('myext', handlebars.engine); 根据http://expressjs.com/3x/api.html#app.engine
  3. 将扩展设置为视图引擎: app.set('view engine', 'myext'); – 不幸的是没有链接到它的工作原理。

问候