nodejs连接mysql登录验证例子
HTML:
<form method="post" action="/logininfo"> <p>用户名:<input type="text" name="username"></p> <p>密码:<input type="text" name="pass"></p> <p><button id="login_bt">登录</button> <button>注册</button></p> </form>
//logininfo
<table border="1">
<tr>
<td>你的帐号</td> <td>{{user}}</td>
</tr>
<tr>
<td>你的密码</td> <td>{{pass}}</td>
</tr>
</table>
<a href="/">返回</a>
路由:
router.post('/logininfo',function(req,res){ //请求 响应
data.query("SELECT liuyan_username,liuyan_userpass FROM nodejs_user WHERE liuyan_username='"+req.body.username+"' AND liuyan_userpass='"+req.body.pass+"' LIMIT 0,1",function(err, rows, fields){
if( typeof rows[0]=='undefined' ){ //输入的帐号密码匹配不到rows[0]=='undefined'
res.render('logininfo',{
titles:'登录信息',
user:'2',
pass:'2'
});
}else{
if(req.body.username==rows[0].liuyan_username && req.body.pass==rows[0].liuyan_userpass){
res.render('logininfo',{
titles:'登录信息',
user:rows[0].liuyan_username,
pass:rows[0].liuyan_userpass
});
}
}
});
});
下载文件:index