var http = require("http");
var url = require("url");
var ejs = require("ejs");
var querystring = require("querystring");
var server = http.createServer(function(req,res){
res.writeHead(200,{"content-type":"text/html;charset=utf-8"});
//当输入 localhost:8000/login 请求登录页面
//当点击 登录按钮 提交数据 action 路径???
var pathname = url.parse( req.url ).pathname;
//获取客户端请求数据的方式 get post
var method = req.method.toLowerCase();
//路由 显示登录页面
if( pathname == "/login" ){
//请求登录页面
ejs.renderFile("views/login.ejs",{},function(err,data){
//注意:如果传送的数据是json套json或者是数组ejs模板只能显示最基本的变量值 object.username 不能显示object类型的数据。
res.end(data);
})
}else if( pathname === "/dologin" && method ==="get" ){ //添加路由 实现登录功能
var query = url.parse( req.url , true).query
//接收客户端请求的数据
console.log(query.username,query.userpwd);
//接收客户端请求的用户名和密码
res.end("dologin");
}else if( pathname === "/dologin" && method ==="post" ){
console.log(999)
//获取post方式提交的数据
//通过data事件 响应数据
var dataStr = "";
req.on("data",function(data){
dataStr += data;
})
//通过end事件 告诉服务器 数据接收完毕
req.on("end",function(){
//console.log(dataStr); username=aaa&userpwd=888
//JSON.parse( '[{}]' ) ('{}')
var json = querystring.parse( dataStr );
// console.log( json.username ,json.userpwd );
//假设 如果 客户端传递的是admin 密码 123456 要求提示登录成功 否则登录失败
if( json.username =="admin" && json.userpwd =="123456" ){
console.log(10000)
res.write("<script>alert('登录成功');location.href='/home'</script>");
//localhost:8000/home
res.end();
}else{
res.write("<script>alert('登录失败');location.href='/login'</script>");
//localhost:8000/home
res.end();
}
})
}else if( pathname === "/home" ){
//请求index.ejs页面
ejs.renderFile("views/home.ejs",{},function(err,data){
res.end(data);
})
}
}).listen(8000);
//服务页
let http = require('http');
let urlStr = require('url'); let model = require('./model'); let server = http.createServer((req,res)=>{
let url = urlStr.parse(req.url,true);
//这是一个url 对象。
if(url.pathname==='/login'&&req.method.toLowerCase()==='get'){
model.login(req,res);
}else if(url.pathname==='/dologin'&&req.method.toLowerCase()==='post'){
model.dologin(req,res);
}
})
server.listen(3000,()=>{
console.log('start');
}) //================
model页面
/**
* this is router handle
*/ let url = require('url');
let ejs = require('ejs');
// 模块依赖项 module.exports = {
login(req,res){
ejs.renderFile('./ejs/login.ejs',{login:'登录'},(err,html)=>{
res.end(html);
})
},
dologin(req,res){
let arr = [
{"pid":1,"pname":"小米1","price":2888},
{"pid":2,"pname":"小米2","price":3888},
{"pid":3,"pname":"小米3","price":4888},
{"pid":4,"pname":"小米4","price":5888},
{"pid":5,"pname":"小米5","price":6888},
]
ejs.renderFile('./ejs/index.ejs',{dologin:'登录成功',goods:arr,year:2011},(err,html)=>{
res.end(html);
})
}
}
主页<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div>
<%=dologin%>
</div>
<%goods.map((message,ind)=>{%>
<div style="float: left;border: 1px solid black">
<%for(i in message){%>
<div style="border: 1px solid black"><%=message[i]%></div>
<%}%>
</div>
<%})%>
<%if(year%4===0&&year%100!==0||year%400===0){%>
<div><%=year%>是闰年</div>
<%}else{%>
<div><%=year%>不是闰年</div>
<%}%>
</body>
</html>
登录页<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<form action="/dologin" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<button> <%=login%></button>
</form>
</body>
</html>