我正在使用express,node.js和mysql,这是我的代码
var bodyParser = require("body-parser");
var bcrypt = require('bcrypt');
var urlEncodedParser = bodyParser.urlencoded({extended: false});
var session = require('client-sessions');
var express = require('express');
var app = express();
//mysql
var mysql = require("mysql");
app.post('/login', function(request, response){
var body = request.body;
connection.query("select * from registeration where username='"+body.username+"' and password='"+body.pwd+"'",function(err, rows){
if(err) {
console.log("username and password are not matched.");
response.end();
} else {
console.log("done");
response.send(rows);
request.usersession.user = user;
response.redirect(301, '/account');
}
});
});
app.get('/account', function (request, response){
if(request.usersession && request.usersession.user) {
response.Send({requ: request.usersession.user});
response.write('user detail ' + request.usersession.user[0]['username']);
console.log('account in');
}else{
console.log('session error');
response.end();
}
});
最佳答案
在尚未通过身份验证时,将重定向添加到/ login:
app.get('/account', function (request, response){
if(request.usersession && request.usersession.user) {
response.Send({requ: request.usersession.user});
response.write('user detail ' + request.usersession.user[0]['username']);
console.log('account in');
}else{
console.log('session error');
// here you should redirect to login:
response.redirect('/login');
response.end();
}
});