Cannot set property'XXX' of undefined in Express-Session and express-session is invalid

TypeError: Cannot set property ‘user’ of undefined

The code points to:

var user={
            username:username,
            password:password,
        }
req.session.user = user; 


Write in app.js (at the same time you don’t need to write it on the routing page, it was originally written on the routing page)

const session = require('express-session');
app.use(cookieParser('session'));
app.use(session({
  saveUninitialized:false,               //是否将刚创建的session存到store中
  resave:false,                          //是否定期更新已经存到store中的session
  secret:"ergwe",                        //用于加密connect.sid(用于寻找session的id,存在于cookie中)
  rolling:true,                          //是否在用户每次请求时重置cookie(connect.sid)的生存时间
  cookie:{maxAge:30*60*1000}             //cookie(connect.sid)的生存时间,也是session的生存时间
}));


The location of the above two lines of code should be written in app.js

app.use(express.static(path.join(__dirname, 'public')));

In front of

Also write the code that defines the route and the code of the 404 page under them

What is the specific principle, I hope the great god will answer

 

 

After solving the above problem, I encountered another problem. Let me change it here. Look at the picture:

 

 

--------------------- 
Author: landing into and 
Source: CSDN 
Original: https: //blog.csdn.net/d13771862759/article/details/79062054 

Guess you like

Origin blog.csdn.net/u012149637/article/details/88669160