cookie的httpOnly属性

cookie的httpOnly属性

前言

cookie 有一个 httpOnly 属性,可以设置一个 只能在服务端设置的cookie 的键值对,即禁止客户端修改携带 httpOnly 属性的 cookie 键值对。

代码

// app.js
const http = require('http')
const userId = 123456
let resData = {
  time: Date.now(),
  age: 11,
  name: 'xiaoming'
}
// 获取cookie的过期时间
const getCookieExpires = () => {
  const d = new Date()
  d.setTime(d.getTime() + (24 * 60 * 60 * 1000))
  return d.toGMTString()
}
const server = http.createServer((req, res) => {
  const method = req.method
  // 设置返回格式为json
  res.setHeader('Content-Type', 'application/json')

  if (method === 'GET') {
    res.setHeader('Set-Cookie', `userid=${userId}; path=/; httpOnly; expires=${getCookieExpires()}`)
    res.end(JSON.stringify(resData))
  }
})

server.listen(8000)
console.log('listening on 8000')

启动一个 node 服务
node app.js

效果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值