为什么在接口中写了set-cookie却不生效

来源:3-7 cookie和session

edgex

2018-06-21

我想模拟一种登录鉴权的场景。

代码是这么写的

index.html中部分代码

<script>
var dom = document.getElementById('content');
fetch('/login').then( (res)=> res.text() ).then( (data) => {
dom.innerHTML = data;
})
</script>

server.js中部分代码

http.createServer((req, res) => {

if (req.url === '/') {
const html = fs.readFileSync('index.html', 'utf8')
res.writeHead(200, {
'content-type': 'text/html',
})
res.end(html)
}

if (req.url === '/login') {
res.writeHead(200, {
'Set-Cookie': 'auth=012390219032139'
})

res.end('success');
}
}).listen('8888')

这是第二次请求接口的network截图

http://img.mukewang.com/szimg/5b2ba8090001906912480544.jpg

问题

  1. 为什么第二次请求/login,request里不带cookie?

  2. 如果使用/login接口设置一次cookie,如何让以后所有请求的其他接口都带cookie值?

写回答

1回答

edgex

提问者

2018-06-21

是fetch的问题。

0
1
Jokcy
好的。
2018-06-25
共1条回复

HTTP协议原理+实践 完整案例解析主流技术

解析HTTP协议原理 夯实HTTP技术基础 打通前后端助你快速成长

4313 学习 · 375 问题

查看课程