用户的注册和登录
前言
本文主要展示结合mysql数据库,axios.js,vue.js后在部署的服务器端,实现用户的注册和登录功能
一、准备工作
1.数据库建表
2.导入相应的模块和文件
npm install express@4 mysql body-parser cors
二、实验结果
三、实验代码(部分)
//校检用户是否存在
app.post("/homepage",function(req,res){console.log(req.body)client.query("select * from user where username=? and password=?",[req.body.username,req.body.password],function(err,results){var message=""if(results.length==0){message="登录失败"}elsemessage="登录成功"res.json({msg:message})})
})
//触发函数(可做参考,读者在此进行相应代码修整,进行登录成功后的页面跳转)new Vue({el:"#app",data:{uname:"",pwd:""},methods:{login(){axios.post("http://127.0.0.1:1000/homepage",{username:this.uname,password:this.pwd}).then(function(res){console.log(res)alert(res.data.msg)})}}
})
总结
Tips:弹出“登录成功”提示框后,读者可以添加相应代码完善login函数(使用window.location.href=""方法可以实现登录成功后,页面的自行跳转);代码并没有完全给出,不过给出的主要代码应该足以读者使用。