my front-end page is in ngnix, and the port is 80
.my background is written in nodejs, and the port is 3060,
now I log in on the login page, request the http://www.xinlizhiyouni.com:80/user/login/phone
interface, then save the data to the session of nodejs, and then jump to the http://www.xinlizhiyouni.com:80/advertise/add.html
request http://www.xinlizhiyouni.com:80/user/logout
interface. But at this time, the session data is undefine,. I would like to ask why my session data has been lost. How to keep the data correctly in session and not lose it after the jump
//
app.all("*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By"," 3.2.1");
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.all("/user/login/phone", (req, res) => {
let sess = req.session;
let param = req.body;
console.log("param == ", param)
let selectParams = {
phone: param.mobilePhoneUsername
};
let userModSql = "SELECT ul.password, ui.uid, ui.name, ui.sex, ui.phone FROM user_information ui, user_login ul WHERE ui.uid=ul.id AND ui.phone=?";
pool.getConnection( (err, connection) => {
if (err) {
throw err;
} else {
connection.query(
userModSql,
[selectParams["phone"]],
function (err, rows) {
if (err) {
throw err;
} else {
console.log("rows === ", rows);
let verifySign = cryptPwd(param.mobilePhonePassword, "liu")
console.log("verifySign === ", verifySign);
if (rows[0].password === verifySign ) {
req.session.loginUserInfo = rows[0]
//cookie
res.cookie("username",rows[0], {maxAge:100000}); //
// res.json({code: 0, message: "success"});
res.redirect("http://www.xinlizhiyouni.com:80/advertise/add.html");
} else {
res.json({code: 1, message: "fail"});
}
}
}
)
}
})
});
app.all("/user/logout", function(req, res, next) {
console.log("loginUserInfo == ", req.session.loginUserInfo);
res.json({ret_code: 0, ret_msg: ""});
});
Code for configuring session
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json());
app.use(cookieParser());
app.use(session({
secret: "123456",
cookie: {maxAge: 60000},
resave: false
}));
< H1 > modified code < / H1 >
let identityKey = "superKey";
app.use(cookieParser());
app.use(session({
name: identityKey,
secret: "heart", // session idcookie
cookie: {maxAge: 60000},
resave: false
}));
//
app.all("*", function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("X-Powered-By"," 3.2.1");
res.header("Content-Type", "application/json;charset=utf-8");
next();
});
app.all("/", function(req, res, next){
let sess = req.session;
let loginUser = sess.loginUser;
let isLogined = !!loginUser;
});
//
app.all("/test/user/login", function(req, res, next){
let sess = req.session;
let user = {
"name": req.body.name
};
console.log("");
sess.loginUser = user.name;
res.json({ret_code: 0, ret_msg: ""});
});
app.all("/test/user/logout", function(req, res, next){
let sess = req.session;
let loginUser = sess.loginUser;
console.log("");
console.log(loginUser);
});