When we write the Web, we will inevitably use cookies, because node. JS has the Express web framework, we can easily build stations. When using Express, this plugin is often used to cookie-parser. Today we will analyze this plugin.
This plugin is often used as a middleware, App.use (Cookieparser ()), so that each requested cookie can be processed. from the name, this is a tool for explaining cookies. The req.cookies can be used to fetch the transmitted cookies and turn them into objects. Below, let's go into its source code. First, we look at Index.jsvar cookie = require (' Cookie '); Var parse = require ('./lib/ Parse '); here are 2 references, one is a cookie and one is the core function of Cookie-parser. This module of cookies provides only 2 methods, one is serialize and the other is parse. The serialize method receives key and Val, and serializes it.  VAR HDR = cookie.serialize (' foo ', ' Bar '); //HDR = ' foo=bar '; paser method turns a string into an object var cookies = Cookie.parse (' Foo=bar; cat=meow; dog= Ruff '); //cookies = {foo: ' Bar ', Cat: ' Meow ', Dog: ' Ruff '}; look below, is Cookie-parser's main function Cookiepaser. exports = Module.exports = function Cookieparser (secret, options) { return function Cookieparser (req, res, NEX T) { //gets Req,res object if (req.cookies) return next () from the request, //if there is already a cookie object, exit the middleware to continue running var cookies = Req.headers.cookie; Take cookie Req.secret from headers = secret; //If there is an incoming secret, set to Req object Req.cookies = Object.create (NULL); //Create an empty object to req.cookies req.signedcookies = object.create (null); Create an empty object for req.signedcookies //No cookies if (!cookies) { //If no COO is obtained from headers kies return next (); //exit middleware continue to run } req.cookies = cookie.parse (cookies, options); //invokes the parse of the cookie to facilitate the transfer of the cookie string to the Cookies object. //parse signed cookies if (secret) { &N Bsp //If Secret is set up, use the 2 methods of parse to sign the cookie. req.signedcookies = Parse.signedcookies (Req.cookies, secret); Req.signedcookies = Parse. Jsoncookies (req.signedcookies); } //Parse JSON cookies REq.cookies = Parse. Jsoncookies (req.cookies); Convert the Req.cookies object Next (); };}; Look at this main function, we still have some confusion, just is its main file parse.js. We'll see what the file is for next time. [cookie Chapter]cookie-parser parser.jshttp://www.cnblogs.com/coolicer/p/4191551.html to HTTP// Www.cnblogs.com/coolicer/p/4191548.html
[Cookie] from Cookie-parser middleware