The parser.js_node.js of Node.js Cookie-parser

Source: Internet
Author: User
Tags cookie names

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.