[Cookie]cookie-parser Parser.js

Source: Internet
Author: User

Cookie-parser's role, the official argument is: The Parse header and populate with a Cookie req.cookies object keyed by the cookie names. my understanding is to convert the cookies in headers and merge them with Req.cookies. As the core of Cookie-parser, parser hints at 2 functions:signedcookies and Jsoncookies, and their helper functions.  This article to the end of this plugin, let us review, but also opened the next session on the study.  Cookie-parser takes a cookie from the client and then passes the Express-session (Express-based) processing. After the client is represented as a signed string, on the server side, because of the use of the Unsign method, you can see the checked characters. In this way, all the fans are untied. Let's take a look at the code of the session that deals with cookies.  function Setcookie (res, name, Val, secret, options) {  var signed = ' s: ' + Signature.sign (Val, secret); //.... Omit    res.setheader (' Set-cookie ', header)} Here write cookies with Set-cookie. When the client sends a cookie, the Parser.js signedcookies is used.  exports.signedcookies = function (obj, secret) {  var cookies = Object.keys (obj);   var dec; & nbsp var key;  var ret = object.create (null);  var val;   for (var i = 0; i < cookies.length; i++) {&nbs P   key = cookies[i];     val = Obj[key];       Dec = Exports.signedcookie (val, secret);       if (Val!== Dec) {&nbsp ;      Ret[key] = dec;      Delete obj[key];   } }   return ret;} ;  exports.signedcookie = function (str, secret) {   return Str.substr (0, 2) = = = ' s: '    ? Signatur E.unsign (Str.slice (2), secret)    : str;}; In the Help function Signedcookie, when the discovery contains S: Start is the signed CoOkie, then used the signature.unsign to sign. Next time, let's take a look at the session.

[Cookie]cookie-parser parser.js

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.