here"s the thing. I want to make a landing page. First of all, A.html is a login page, and login is a post request. After a successful login, the server writes the login name of the req.session.LoginName=" user. "the front end jumps to the B page, and the B page first initiates a get request, and then the server checks whether req.session.LoginName returns req.session.LoginName, but the actual output is that req.session is always undefined.
. Core Code:
[app.js]
var express = require("express");
var app = express();
var router = require("./router/router.js");
var session = require("express-session");
app.use(session({
secret: "keyboard cat",
//name: "connect.sid", //namecookienamecookiename:connect.sid
cookie: {maxAge: 80000 }, //maxAge80000ms80ssessioncookie
resave: false,
saveUninitialized: true
}));
//
app.post("/doLogin",router.doLogin);
app.get("/checkloginBysession",router.checkloginBysession);
[router.js]
exports.doLogin = function (req, res, next){
//...
req.session.LoginName=username.toString();
console.log(""+req.session.LoginName);//
res.end("{"state":"1","msg":""}");
}
exports.checkloginBysession =function(req, res, next) {
//req.session.LoginName=username.toString();
res.writeHead(200,{"Content-type":"application/json;charset=UTF8","Access-Control-Allow-Origin":"*"});
console.log(""+req.session.LoginName);//undefined
if (req.session.LoginName) {
res.end("{"state":"1","msg":"session"}");
return;
}
else
{
res.end("{"state":"0","msg":""}");
return;
}
}
Thank you in advance ~