CookieParser 미들웨어는 웹 브라우저에서 보낸 쿠키의 내용을 얻는 데 사용됩니다. CookieParser 미들웨어를 사용한 후
클라이언트를 대신하여 요청 된 htto.incomingmessage 객체에는 쿠키 속성이 있으며, 이는 객체 배열입니다.
웹 브라우저에서 전송 된 모든 쿠키를 저장하고 각 쿠키는 쿠키 속성 값 배열의 객체입니다.
index.html 코드 :
코드 사본은 다음과 같습니다.
<! doctype html>
<html>
<head lang = "en">
<meta charset = "utf-8">
<title> 파일 업로드 서버 </title>
<script type = "text/javaScript">
함수 제출 쿠키 () {
var xhr = 새로운 xmlhttprequest ();
xhr.open ( "post", "index.html", true);
document.cookie = "FirstName = sisi";
document.cookie = "username = dr.";
xhr.onload = function (e) {
if (this.status == 200)
document.getElementById ( "res"). innerHtml = this.Response;
};
xhr.send ();
}
</스크립트>
</head>
<body>
<H1> CookieParser 미들웨어 사용 </h1>
<입력 유형 = "button"value = "쿠키 제출"onclick = "submitcookie ();"; />
<div id = "res"> </div>
</body>
</html>
Server.js 코드 :
코드 사본은 다음과 같습니다.
var express = 요구 사항 ( "Express");
var fs = 요구 ( "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 ( "쿠키 이름 :"+key);
res.write ( ", 쿠키 값 :"+req.cookies [key]+"<br />");
}
res.end ();
});
app.listen (1337, "127.0.0.1", function () {
Console.log ( "청취 시작 1337");
});
테스트 결과