经过几天的实验加搜索,终于知道一个中间件可以解决这个问题了
npm install consolidate
consolidate传送门 传送门2
使用说明传送门
快照:ahjesus
Since doT (and probably your template engine of choice, as well) is accessed through consolidate, consolidate is the only additional module I need to require at the top ofserver.js
:
var express = require( "express" ),
app = express(),
cons = require( "consolidate" );
I want to continue serving some of my other pages statically, so I add my template configuration stuff below the existing app.use
line in my code:
app.use( express.static( _dirname + "/public" ) );
app.engine( "dot", cons.dot );
app.set( "view engine", "dot" );
app.set( "views", _dirname + "/public/views" );
Those three new lines set doT (as exposed by consolidate) as the view engine, register files ending in .dot
as templates, and tell Express to look in /public/tmpl
for templates to use. So when Node sees res.render( "detail", { ... } )
, it knows to expand"detail"
to /public/tmpl/detail.dot
and render it as a doT template. Now I can restart my server, go to http://localhost:3000/product/102, and see my template rendered statically, without creating a separate server-side file.