Introduction to Node.js Cookie-parser middleware _node.js

Source: Internet
Author: User

Prior to joining a learning notebook group, by learning notes to share learning results. Send a copy here, too.

When we write the web, it is inevitable to use cookies, because Node.js has the express this web framework, we can easily to build a station. When using express, it is often used to cookie-parser this plugin. Today we will analyze this plugin.

This plugin is typically used as a middleware, App.use (Cookieparser ()) so that you can process each request cookie.

From the name, this is a tool to explain cookies. Through the req.cookies, you can take the cookie that comes in and turn it into an object. Below, let's go deep into its source code.

first, let's look at Index.js

Copy Code code as follows:
var 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. Cookie This module, only provides 2 methods, one is serialize the other is parse.

The Serialize method receives the key and Val and serializes it.

Copy Code code as follows:
var hdr = cookie.serialize (' foo ', ' Bar '); HDR = ' Foo=bar ';

Paser method to turn strings into objects

Copy Code code as follows:
var cookies = cookie.parse (' Foo=bar; cat=meow; Dog=ruff '); cookies = {foo: ' Bar ', Cat: ' Meow ', Dog: ' Ruff '};

then look at the following, which is the main function of the Cookie-parser cookiepaser.

Copy Code code as follows:
Exports = Module.exports = function Cookieparser (secret, options) {
return function Cookieparser (req, res, next) {//Get Req,res object from request
if (req.cookies) return next (); If there is already a cookie object, exit the middleware and continue running
var cookies = Req.headers.cookie; To fetch cookies from the headers

Req.secret = secret; If there is an incoming secret, set to the Req object
Req.cookies = Object.create (null); Create an empty object for Req.cookies
Req.signedcookies = Object.create (null); Create an empty object for Req.signedcookies

No cookies
if (!cookies) {//If no cookies are received from headers
return next (); Exit middleware and Continue running
}

Req.cookies = Cookie.parse (cookies, options); The parse that invokes the cookie facilitates the conversion of the cookie string into a cookies object.

Parse signed Cookies
if (secret) {//If secret is set, the cookie is signed by using the 2 methods of parse.
Req.signedcookies = Parse.signedcookies (Req.cookies, secret);
Req.signedcookies = Parse. Jsoncookies (req.signedcookies);
}

Parse JSON Cookies
Req.cookies = Parse. Jsoncookies (req.cookies); Transform the Req.cookies object into

Next ();
};
};

Look at this main function, we still have some confusion, just is its main file parse.js. Let's see what this file is for next time.

The above is Node.js Cookie-parser middleware All content, hope can give everybody a reference, also hope everybody support cloud habitat community.

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.