注意:以下是在Windwo環境下
運行:
npm install ejs
然后你的目錄node_modules下將增加ejs文件夾
app.js
var express = require("express"); var app = express(); //指定模板引擎 app.set("view engine", 'ejs'); //指定模板位置 app.set('views', __dirname + '/views'); //利用模板文件home.ejs渲染為html app.get("/", function(req, res) { res.render('home.ejs', { name: 'tinyphp' }); }); var server = app.listen(3000, function() { console.log("請在瀏覽器訪問:http://localhost:3000/"); });
home.ejs ,模板寫法可參考官方:http://www.embeddedjs.com/ 或:http://ejs.co/
<html> <head> <title>my ejs template</title> </head> <body> <p>Hi <%= name %></p> </body> </html>
node下app.js后,訪問:http://localhost:3000/

上面不想手動創建文件夾和文件的,可以參考以下命令:
新建文件夾
mkdir views
新建文件
cd.>home.ejs
想必聰明的你會發現jade和ejs上調用過程是一樣的,只是模板的寫法不同。
