koajs / koajs/session

sesstion 设置之后没有同步到cookie里面

Open
#120 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
908
Forks
115
PR merge metrics
No merged PRs in 30d

Description

所以session无法记录
非常神奇的一个问题。
我的koa-router配置文件如下

![image](https://user-images.githubusercontent.com/14355994/39687311-60bba4e4-5200-11e8-9435-d0275f484263.png)

```js
// index.js
const Api = require('./api.js');
const Upload = require('./upload');
const Auth = require('./auth');
const router = require('koa-router')();
const koaBody = require('koa-body');

router
.get('/api', Api)
.all('/auth', Auth)
.post('/upload', koaBody({ multipart: true }), Upload)
.get('/test1',function(ctx,next){
ctx.body = '123'
ctx.session.hee = ctx.session.hee+1 || 0;
})
// .get('/test2',function(ctx,next){
// ctx.body = ctx.session.hee
// })

module.exports = router;
```

```js
const axios = require('axios');
const Api = async function(ctx,next) {
axios({
url: 'http://192.168.1.14:8080/daocheng/a/mall/105293660609/get_goods_list_shop',
method: "post",
params: {
order: '',
nopage: 1,
corp_pri: 0,
pageSize: 15,
goods_name: '',
_extfn: 'get_shop_goods_by_type_list2',
id: '9ae4ca9d069547c19deb61f010518ec0',
},
headers: {
"Cookie": ctx.session.javaCookie || null,
}
})
.then(async res => {
if(!ctx.session.javaCookie) {
ctx.session.javaCookie = res.headers['set-cookie'] ? res.headers['set-cookie'] : ''
}
console.log(res.headers['set-cookie']);
console.log('正确')
// console.log(response.data)
ctx.session.hee = ctx.session.hee+1 || 0;
console.log(ctx.session);
await next();
})
.catch(error => {
console.log('错误')
console.log(error.response)
});
};
module.exports = Api;
```
![image](https://user-images.githubusercontent.com/14355994/39687443-0de8c0f2-5201-11e8-8637-a6df91f31960.png)

非常神奇,我在axios里面修改ctx.session.hee居然不会被保存

但是在下面这个index.js这个地方修改ctx.session.hee又是可以同步cookie。

![image](https://user-images.githubusercontent.com/14355994/39687492-41f3fed4-5201-11e8-9187-0c11b84ef781.png)

Contributor guide

Open the contributing guide

Research direction

Review the session updates in index.js and api.js, especially the axios callback where ctx.session.hee and ctx.session.javaCookie are changed. Trace how the request lifecycle saves session state after the callback, then reproduce the difference between /test1 and Api and add a regression test showing the expected persistence.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.