[英]validation of password and username nodejs mysql
I am working on a project and I'm trying to build a login page on nodejs-html by using mysql as database. 我正在一个项目上,我正在尝试使用mysql作为数据库在nodejs-html上构建一个登录页面。 But my code directly gets to "response.send('Please enter Username and Password!');"
但是我的代码直接到达“ response.send('请输入用户名和密码!');” part, and shows me that page.(no problem with mysql connection) Why can't i check if the login username and password is right?
部分,并向我显示该页面。(mysql连接没有问题)为什么我不能检查登录用户名和密码是否正确? And go to the next page if true?
如果是,请转到下一页?
var mysql = require('mysql');
var express = require('express');
var session = require('express-session');
var bodyParser = require('body-parser');
var path = require('path');
var connection = mysql.createConnection({
....................
});
connection.connect((err) => {
if(err){
throw err;}
else console.log("connected");
});
var app = express();
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({extended : true}));
app.use(bodyParser.json());
app.get('/', function(request, response) {
response.sendFile(path.join(__dirname + '/login.html'));
});
app.post('/', function(request, response) {
var username = request.body.user_name;
var password = request.body.pass;
console.log(username);
if (username && password) {
connection.query('SELECT * FROM users WHERE user_name = ? AND pass = ?', [username, password], function(error, results, fields) {
console.log(username);
if (results.length > 0) {
request.session.loggedin = true;
request.session.user_name = username;
response.redirect('/secondPage');
} else {
response.send('Incorrect Username and/or Password!');
}
response.end();
});
} else {
response.send('Please enter Username and Password!');
response.end();
}
});
app.get('/login', function(request, response) {
if (request.session.loggedin) {
response.send('Welcome back, ' + request.session.username + '!');
} else {
response.send('Please login to view this page!');
}
response.end();
});
app.listen(3000);
.body.user_name is undefined that's why
you must have forgot name attribute in your html 您必须在HTML中忘记了name属性
input type="text" name="user_name"
输入type =“ text” name =“ user_name”
also password 也是密码
<input type="password" name="pass" >
give them suitable name atribute so that body object can access them 给它们适当的名称属性,以便正文对象可以访问它们
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.