使用nodejs,ajax,mongoose实现登陆注册功能,带crypto加密功能

3 篇文章 0 订阅
2 篇文章 0 订阅

//注册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)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值