express的中间件cookieParser详解
中间件 详解 Express
2023-06-13 09:15:32 时间
cookieParser中间件用于获取web浏览器发送的cookie中的内容.在使用了cookieParser中间件后,
代表客户端请求的htto.IncomingMessage对象就具有了一个cookies属性,该属性之为一个对象的数组,
其中存放了所有web浏览器发送的cookie,每一个cookie为cookies属性值数组中的一个对象.
index.html代码:
<!DOCTYPEhtml>
<html>
<headlang="en">
<metacharset="UTF-8">
<title>向服务器上传文件</title>
<scripttype="text/javascript">
functionsubmitCookie(){
varxhr=newXMLHttpRequest();
xhr.open("post","index.html",true);
document.cookie="firstName=思思";
document.cookie="userName=博士";
xhr.onload=function(e){
if(this.status==200)
document.getElementById("res").innerHTML=this.response;
};
xhr.send();
}
</script>
</head>
<body>
<h1>cookieParser中间件的使用</h1>
<inputtype="button"value="提交cookie"onclick="submitCookie();"/>
<divid="res"></div>
</body>
</html>
server.js代码:
varexpress=require("express");
varfs=require("fs");
varapp=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(varkeyin req.cookies){
res.write("cookie名:"+key);
res.write(",cookie值:"+req.cookies[key]+"<br/>");
}
res.end();
});
app.listen(1337,"127.0.0.1",function(){
console.log("开始监听1337");
});
测试结果
相关文章
- 通过中间件添加用户的Claim
- Golang框架Gin入门实战--(8)Gin中间件详解 路由中间件 全局中间件 路由分组中间件
- 用科创板给国产中间件打上一剂兴奋剂!
- 这种软件中间件你知道吗?
- Scrapy中间件采集HTTPS网站失败的原因
- 当当开源sharding-jdbc,轻量级数据库分库分表中间件详解数据库
- 分库分表数据库中间件对比详解数据库
- laravel中间件使用详解编程语言
- Go中的http服务流程、路由以及http中间件的使用详解编程语言
- MySQL开放源码中间件:实现数据库灵活性的好选择(mysql开源中间件)
- 使用Linux 实现中间件利器(中间件linux)
- SQL Server中间件:为企业应用提供安全的支撑(sqlserver中间件)
- 使用Redis代理构建高效中间件架构(中间件架构redis代理)
- Oracle中间件配置提升企业数据库系统性能(oracle中间件配置)
- Oracle中间件助你实现梦想的利器(oracle中间件好用吗)
- express的中间件basicAuth详解
- express的中间件bodyParser详解