Home  >  Article  >  Web Front-end  >  Detailed explanation of express's middleware cookieParser_node.js

Detailed explanation of express's middleware cookieParser_node.js

WBOY
WBOYOriginal
2016-05-16 16:29:051580browse

The cookieParser middleware is used to obtain the content in the cookie sent by the web browser. After using the cookieParser middleware,

The htto.IncomingMessage object representing the client request has a cookies attribute, which is an array of objects,

It stores all cookies sent by web browsers. Each cookie is an object in the cookies attribute value array.

index.html code:

Copy code The code is as follows:





Upload files to the server



Usage of cookieParser middleware






server.js code:

Copy code The 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] "
");
}
res.end();
});
app.listen(1337,"127.0.0.1", function () {
console.log("Start monitoring 1337");
});

Test results

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn