首页 > 其他 > 详细

Cookie

时间:2019-03-10 14:42:26      阅读:164      评论:0      收藏:0      [点我收藏+]

一、Cookie介绍

通过Set-Cookie设置

下次请求会自动带上

键值对,可以设置多个

 

二、Cookie属性

max-age和expires设置过期时间

Secure只在https的时候发送

HttpOnly无法通过document.cookie访问

 

三、Cookie的使用方式

 Server.js

const http = require(‘http‘);
const fs = require(‘fs‘)

http.createServer(function(request, response){
    console.log(‘request com‘, request.url)
    if(request.url === "/"){
        const html = fs.readFileSync(‘test.html‘,‘utf8‘)
        response.writeHead(200,{
            ‘Content-Type‘:‘text/html‘,
            ‘Set-Cookie‘:‘id=123456‘
        })
        response.end(html)
    }


    


}).listen(8888);

console.log(‘server listening on 8888‘)
 
设置HttpOnly,JS将无法访问abc这个Cookie值
‘Set-Cookie‘:[‘id=123456; max-age=2‘, ‘abc=456; HttpOnly‘]

  

 

test.html

<html>
    <head>
        <title>Document</title>
    </head>
    <body>
        <div>Content</div>
    </body>

    <script>
        console.log(document.cookie);
    </script>
    
    
</html>

  

访问,可以看到设置的Cookei

技术分享图片

 

Cookie

原文:https://www.cnblogs.com/linlf03/p/10505211.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!