node --- 在express中配置使用模板引擎(art-template)
生活随笔
收集整理的這篇文章主要介紹了
node --- 在express中配置使用模板引擎(art-template)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
下載依賴:
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
目錄結構:
總結
以上是生活随笔為你收集整理的node --- 在express中配置使用模板引擎(art-template)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 1037C. Equalize
- 下一篇: 元素(Element)