var http=require('http');
var ejs=require('ejs');
// console.log(path.extname)
var router=require('./model/router.js');
//console.log(mimeModel.getMime(fs,'.html'));
var url=require('url');
//根据不同的url 执行不同的业务逻辑
http.createServer(function(req,res){
// router.statics(req,res,'static');
//login登陆的一些功能 register 注册的功能
// console.log(req.url);
res.writeHead(200,{"Content-Type":"text/html;charset:utf-8"});
var pathname=url.parse(req.url,true).pathname;
var method=req.method.toLocaleLowerCase();
console.log(method);
if(pathname=='/login'){//显示登陆页面
ejs.renderFile('views/form.ejs',
{
},function(err,data){
res.end(data);
})
}else if (pathname=='/dologin'&& method=='get'){
//get 方式获取数据
console.log(url.parse(req.url,true).query);
res.end('dologin get');
}else if (pathname=='/dologin'&& method=='post'){
//post方式获取数据
var postStr='';
req.on('data',function(chunk){
postStr+=chunk;
})
req.on('end',function(err,chunk){
// postStr+=chunk;
res.end("<script>alert('登陆成功');history.back();</script>");
})
}
else{
ejs.renderFile('views/index.ejs',
{
},function(err,data){
res.end(data);
})
}
}).listen(8881);
form.ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>登录</h2>
<form action="/dologin" method="post">
<input type="text" name="username"/>
<br/>
<input type="password" name="password"/>
<input type="submit" value="登录"/>
</form>
</body>
</html>
login.ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h2>这是一个ejs的后台模板引擎__登陆</h2>
<h2><%=msg%></h2>
<br/>
<hr/>
<ul>
<% for(var i=0;i<list.length;i++){ %>
<li> <%=list[i]%></li>
<% }%>
</ul>
</body>
</html>