Connect middleware session cookie usage method sharing

  • 2020-03-30 03:22:30
  • OfStack

Connect is a node middleware framework. Specific table, see the website http://www.senchalabs.org/connect/

Under the configuration in app.js (must be put into app.use(routes); Before)


var connect = require("connect");
app.use(connect.cookieParser());
app.use(connect.session({ secret: 'jiami', cookie: { maxAge: 60*60*24*1000}}));

And then it's used in the controller


// Set up the 
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


//The cancellation of the session
req.session.destroy(function(err){
 console.log(err);
})
//Rebuild sid
req.session.regenerate(function(err){
 console.log(err);
});

When the session is set, we see that there is an additional sid in the cookie 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 is talking to the client by storing a connect.sid, but the session is stored in memory

Second, cookies, official documents: (http://www.senchalabs.org/connect/cookieParser.html this document pit I spent the whole day time), do a lot of talking not table, try the following example is elder brother, because didn't write on the document using method

Set up the


//Set the cookie
res.cookie("user",{username:"cookie_name",password:"cookie_pwd"},{ maxAge: 60*60*24*1000,httpOnly:true, path:'/'});
res.cookie("msg", " The username or password cannot be null ", {maxAge:60*60*24*1000});

delete


res.cookie("msg", " The username or password cannot be null ", {maxAge:0});


Related articles: