The nature of the session is implemented using cookies.
The principle is probably: HTTP brings the server to set the cookie in advance, the server gets the cookie that identifies the user, and then retrieves the corresponding user identity from the fixed location (database, file). Assign the identity to this request, in the process of processing will know the identity of the user. (It is automatically implemented in php,asp or other service-side languages)
Implementing cookies
You need to set up a cookie that identifies the user for each user. You can use the following rules
Registered mailbox MD5 value + Password MD5 value + Random code MD5 value. (Just for example, this may not be a good plan)
Service-Side code fragment:
Copy Code code as follows:
Res.setheader ("Set-cookie", [sid= "+newuser.tocookie () +";p ath=/;d omain= "+config.domain+"; expires= "+new" Date (" 2030 ")]);
Cookies
Copy Code code as follows:
Sid=275fccab7935736ff68c95c3ddbfaaee|275fccab7935736ff68c95c3ddbfaaee|275fccab7935736ff68c95c3ddbfaaee
Use cookies to get user identities, set session
All requests for non-static resources are directed to this process. Gets the cookie, splits the cookie, and finds the eligible user in the database. Finally, use next to jump to the next request logic.
The next request logic simply uses Req.session.user to get the user object.
Copy Code code as follows:
Session:function (req, res, next) {
Req.session = {};
if (req.cookies && req.cookies.sid) {
var a = Req.cookies.sid.split ("|");
var hexmail = a[0];
var hexpwd = a[1];
var hexrandom = a[2];
usermodel.hexfind (Hexmail, hexpwd, Hexrandom, function (status) {
//console.log ("Hexfind", status);
if (Status.code = "0") {
//req.cookiesselecter = Cookiesselecter;
Req.session.user = Status.result;
}
next ();
});
}else{
Next ();
}
}
The above is Nodejs session simple use of all the content, hope to give you a reference, but also hope that we support cloud habitat community.