我用nodejs和mysql创建了一个登录系统,用户在其中输入用户名和密码。问题是,当用户输入错误的用户名或密码时,系统会警告用户,但是如果详细信息正确,则节点服务器应呈现主页,但实际上没有任何反应。请帮忙!下面是我的服务器代码:
class Solution {
public:
bool cmp(pair<int,int> a, pair<int, int> b)
{
if(a.first!=b.first) return a.first < b.first;
else return a.second > b.second;
}
vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {
vector<vector<int>> rlt(people.size());
sort(people.begin(), people.end(), cmp);
...
}
};
下面是我的客户端代码:
var express=require('express');
var app=express();
var mysql=require('mysql');
var path=require('path');
var bodyParser=require('body-parser');
var session=require('express-session');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(__dirname+'/public'));
app.set('view engine','ejs');
var connection=mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'vipul1234',
database: 'employee'
});
connection.connect((function(err){
if(err){
console.log('error connecting;'+err.stack);
}
else{
console.log('connected as id:'+connection.threadID);
}
}));
app.use(session({
secret : "secret_password",
resave : true,
saveUninitialized : true
}));
app.get('/',function(req, res){
res.sendFile(path.join(__dirname+'/login.html'));
});
app.get('/home',function(req, res){
if(req.session.loggedin!=true){
return res.redirect('/login.html');
res.end();
}
else{
console.log("hello");
return res.render('dashboard',{username: req.session.usernm});
}
res.end();
});
app.post('/auth',function(req, res){
console.log(req.body);
var usernm=req.body.usrnm;
var passwd=req.body.passwrd;
console.log(usernm);
var obj={};
if(usernm&&passwd){
var sql="select * from signup where username='"+usernm+"' and password='"+passwd+"';";
connection.query(sql, function(err, result){
if(err) throw err;
if(result.length!=0)
{
req.session.loggedin=true;
req.session.username=usernm;
return res.redirect('/home');
}
else{
obj={'message':'Incorrect Username and/or Password'};
}
obj=JSON.stringify(obj);
res.send(obj);
});
}
});
app.listen(3000);
$(function(){
$('#submitbut').on('click',function(event){
var obj={};
var usrnm=$('#usernm').val();
var passwrd=$('#passwd').val();
obj.usrnm=usrnm;
obj.passwrd=passwrd;
$.ajax({
type: 'POST',
dataType: 'json',
data: obj,
url: 'http://localhost:3000/auth',
success: function(response) {
alert(response.message);
}
});
});
});