Express middleware cookieParser details, middleware cookieparser

Source: Internet
Author: User

Express middleware cookieParser details, middleware cookieparser

CookieParser middleware is used to obtain the content in cookies sent by web browsers. After cookieParser middleware is used,

The htto. IncomingMessage object requested by the client has a cookie attribute, which is an array of objects,

It stores all cookies sent by web browsers. Each cookie is an object in the array of cookies property values.

Index.html code:

Copy codeThe Code is as follows:
<! DOCTYPE html>
<Html>
<Head lang = "en">
<Meta charset = "UTF-8">
<Title> upload a file to the server </title>
<Script type = "text/javascript">
Function submitCookie (){
Var xhr = new XMLHttpRequest ();
Xhr. open ("post", "index.html", true );
Document. cookie = "firstName = Sisi ";
Document. cookie = "userName = Phd ";
Xhr. onload = function (e ){
If (this. status = 200)
Document. getElementById ("res"). innerHTML = this. response;
};
Xhr. send ();
}
</Script>
</Head>
<Body>
<H1> Use of cookieParser middleware <Input type = "button" value = "Submit cookie" onclick = "submitCookie ();"/>
<Div id = "res"> </div>
</Body>
</Html>

Server. js code:

Copy codeThe Code is as follows:
Var express = require ("express ");
Var fs = require ("fs ");
Var app = express ();
App. use (express. cookieParser ());
App. get ("/index.html", function (req, res ){
Res. sendfile (_ dirname + "/index.html ");
});
App. post ("/index.html", function (req, res ){
For (var key in req. cookies ){
Res. write ("cookie name:" + key );
Res. write (", cookie value:" + req. cookies [key] + "<br/> ");
}
Res. end ();
});
App. listen (1337, "127.0.0.1", function (){
Console. log ("Start listening 1337 ");
});

Test Results

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.