//注册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>post注册</title>
</head>
<body>
<input type="text" class="uname">
<input type="password" class="upwd">
<button>注册</button>
<div class="box"></div>
</body>
</html>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script>
$("button").click(function(){
$.ajax({
url:"http://localhost:3002/register",
data:{name:$(".uname").val(),pwd:$(".upwd").val()},
type:"post",
success(date){
//根据后台返回的数据,在前台页面显示相应的效果
if(date.status==200 && date.content==1){
alert("注册成功")
var i=5;
//注册成功,倒计时之后跳转页面
setInterval(function(){
i--;
if(i==0){
window.location.href="login.html"
return
}
var newHtml="<p>"+i+"秒后自动跳转到登陆页面</p>";
$(".box").html(newHtml);
},1000)
}else if(date.status==500 && date.content==0){
alert("账户已存在,请重新输入")
}else if(date.status==500 && date.content==2){
alert("注册失败")
}
}
})
})
</script>
//登陆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>
<input type="text" class="uname">
<input type="password" class="upwd">
<button>登陆</button>
</body>
</html>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
<script>
$("button").click(function(){
$.ajax({
url:"http://localhost:3002/login",
data:{name:$(".uname").val(),pwd:$(".upwd").val()},
type:"post",
success(data){
if(data.content==1){
alert("登陆成功")
}else if(data.content==0){
alert("账号或密码错误")
}
}
})
})
</script>
//js代码
var express=require("express");
var app=express();
var bodyParser=require("body-parser");
var mongoose=require("mongoose");
var crypto=require("crypto")
var uE=bodyParser.urlencoded({extended:false});
//解决跨域
app.use(function (req,res,next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE,OPTIONS');
next();
})
//创建模板
const Client =mongoose.model("cliente",{
name:String,
pwd:String
})
//注册的后台功能
app.post("/register",uE,function(req,res){
//获取前台发过来的数据
var name=req.body.name
var pwd=req.body.pwd
//加密
var md5Pwd= crypto.createHash('md5').update(pwd).digest('hex');
//链接数据库
mongoose.connect("mongodb://localhost:27017/cs",{useNewUrlParser:true},function (err) {
if(err){
console.log("连接失败")
}else{
console.log("连接成功")
//注册之前先查询数据库里是否被注册过
Client.find({name:name,pwd:pwd}).count().then((ok)=>{
console.log("ok")
if(ok>0){
console.log("账户已存在,请重新输入")
res.send({status:500,content:0})
}else{
//设置插入内容
const client=new Client({
name:name,
pwd:md5Pwd
})
//使用sava()进行插入
client.save().then((ok)=>{
console.log("注册成功");
res.send({status:200,content:1})
}),(err)=>{
console.log("注册失败")
res.send({status:500,content:2})
}
}
})
}
})
})
//登陆的后台功能
app.post("/login",uE,function(req,res){
var uname=req.body.name
var upwd=req.body.pwd
//加密
var md5Pwd= crypto.createHash('md5').update(upwd).digest('hex');
mongoose.connect("mongodb://localhost:27017/cs",{useNewUrlParser:true},function(err){
if(err){
console.log("连接失败")
}else{
console.log("连接成功")
Client.find({name:uname,pwd:md5Pwd}).then((ok)=>{
console.log("ok")
if(ok.length==1){
console.log("登陆成功")
res.send({status:200,content:1})
}else{
console.log("登陆失败")
res.send({status:500,content:0})
}
})
}
})
})
app.listen(3002)