下載依賴:
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
目錄結構: