下载依赖:
npm install --save art-template express-art-template
配置:
// app.js
const express = require("express");
const app = express();
app.engine("html", require("express-art-template"));
使用: 例如处理浏览器GET请求 /students/new,读取new.html中得元素,使用模板引擎渲染后,返回给浏览器
// app.js
const express = require("express");
const fs = require("fs");
const app = express();
const router = express.Router();
app.engine("html", require("express-art-template"));
app.use(router);router.get("/students/new",function(req,res) {res.render("new.html",{msg:"Hi art-template"});
})app.listen(3000, function(){console.log("Server running...");
});
// new.html (注:模板引擎默认会相对views目录下读取文件,即此时目录结构是 views/new.html)
<h1>{{ msg }}</h1>
启动服务器. node app.js
在浏览器输入 http://localhost:3000/student/new
目录结构: