Cookie-parser's role, the official saying is: the Parse cookie header and populate req.cookies with a object keyed by the cookie names. My understanding is that the shift head Cookies in the ERS and merge with Req.cookies. As the core of Cookie-parser, Parser prompts 2 functions: Signedcookies and Jsoncookies, and their help functions.
This article to the end of this plugin, let us review, but also open the next session on the study.
Cookie-parser the cookie from the client and then passes the express-session (Express-oriented) processing. When the client is represented as a signed string, the unsign character is visible at the server because of the use of the method. As a result, all the fans were untied. Let's take a look at the session's code to process cookies.
Copy Code code as follows:
function Setcookie (res, name, Val, secret, options) {
var signed = ' s: ' + Signature.sign (Val, secret);
// .... Omitted
Res.setheader (' Set-cookie ', header)
}
cookies are written here using Set-cookie. When the client sends a cookie, the Parser.js signedcookies is used at this time.
Copy Code code as follows:
exports.signedcookies = function (obj, secret) {
var cookies = Object.keys (obj);
var Dec;
var key;
var ret = object.create (null);
var Val;
for (var i = 0; i < cookies.length; i++) {
key = Cookies[i];
val = Obj[key];
Dec = Exports.signedcookie (val, secret);
if (Val!== Dec) {
Ret[key] = Dec;
Delete Obj[key];
}
}
return ret;
};
Exports.signedcookie = function (str, secret) {
Return Str.substr (0, 2) = = ' s: '
? Signature.unsign (Str.slice (2), secret)
: STR;
};
In the Help function Signedcookie, when a cookie is found that contains s: The beginning is signed, then a signature.unsign is used. Next time, let's take a look at the session.
The above is Node.js cookie-parser parser.js all content, hope to give you a reference, also hope that we support cloud-dwelling community a lot.