1 安装express 生成器
npm install express-generator -g
1 demo.js
var express = require('express');
var ap = express();
app.use(express.static('public'));
app.get('/',function(req,res){
res.send('Hello World');
})
app.get('/hello',function(req,res){
fs.readFile("ejs02.ejs","utf-8",function(err,data){
res.writeHead(200, {'Content-Type': 'text/html'");
var title = 'Hello world';
var userInfo = {
name: "devill15th",
age:5
};
var html = ejs.render(data,{title:title, userInfo:userInfo});
res.end(html);
});
var server = app.listen(8081, function(){
var host = server.address().address
var port = server.address().port
console.log("http://%s:%s",host,port)
});
2 ejs02.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<title><%=title%></title>
<meta charset="utf-8"/>
</head>
</body>
name:<%=userInfo.name %><br/>
age:<%=userInfo.age %><br/>
</body>