Connect is a node middleware framework. For details, please refer to the official website http://www.senchalabs.org/connect/
Configure in app.js (must be placed before app.use(routes);)
var connect = require("connect");
app.use(connect.cookieParser());
app.use(connect.session({ secret: 'jiami', cookie: { maxAge: 60*60*24*1000}}));
Then use
in the controller
//Set
req.session.username= "sess_username";
req.session.password="sess_admin";
req.session.your = {username:"sess_name",password:"sess_pwd"};
//Use
console .log(req.session.username);
console.log(req.session.your);
Other methods
//Log out session
req.session.destroy (function(err){
console.log(err);
})
//Regenerate sid
req.session.regenerate(function(err){
console.log( err);
});
When the session is set, we see that there is an extra sid in the cookie, which is to record the session ID
Print req.cookies and req.session objects
console.log(req.cookies);
console.log(req.session);
You can see that the session talks to the client by saving a connect.sid, but the session is stored in memory
2. Cookie, official document: (http://www.senchalabs.org/connect/cookieParser.html This document fooled me for a whole day), it goes without saying that the following example was tried by me, because There is no usage method in the document
Settings
//Set cookie
res.cookie(" user",{username:"cookie_name",password:"cookie_pwd"},{ maxAge: 60*60*24*1000,httpOnly:true, path:'/'});
res.cookie("msg" , "Username or password cannot be empty", {maxAge:60*60*24*1000});
Delete
res.cookie("msg", "Username or Password cannot be empty", {maxAge:0});