Home > Web Front-end > JS Tutorial > Share the usage methods of connect middleware session and cookie_node.js

Share the usage methods of connect middleware session and cookie_node.js

WBOY
Release: 2016-05-16 16:44:02
Original
1861 people have browsed it

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);)

Copy code The code is as follows:

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

Copy code The code is as follows:

//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

Copy code The code is as follows:

//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

Copy code The code is as follows:

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

Copy code The code is as follows:

//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

Copy code The code is as follows:

res.cookie("msg", "Username or Password cannot be empty", {maxAge:0});

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template