前端实现
前端利用axios插件向服务器发送请求,用post方法,post方法一般用于提交,post向后端提交两个参数userName和userPwd
login(){
if(!this.userName||!this.userPwd){
this.errorTip = true;
return;
}
axios.post('/users/login',{//向服务端发送请求
userName:this.userName,//post参数,get加param
userPwd:this.userPwd
}).then((response)=>{//promise结构
let res = response.data;//接受后端传来的参数
if(res.status ==="0"){
this.errorTip = true;//登录成功,隐藏错误提示
this.loginModalFlag = false;//关闭窗口
}else{
this.errorTip = true;//登陆失败,显示错误提示
}
})
}
服务器端
服务器端首先通过request拿到前端请求的参数,利用模型去查询mongodb的API,若失败,返回失败信息;成功,返回用户名。
router.post("/login",function (