1、安装引用koa-session
npm install --save koa-session
var session = require('koa-session');
2、配置session
app.key = ['some secret hurr']; /*cookie的签名*/
const CONFIG = {
key:'koa:sess', /*默认*/
maxAge:60000 /*同cookie 【需要修改】*/
overwrite:true /*同cookie*/
httpOnly:true /*同cookie*/
signed:true /*默认签名*/
rolling:false /*在每次请求时强行设置cookie,这将重置cookie过期时间(默认:false)【需要修改】*/
renew:true /*renew session when session is nearly expired 【需要修改】*/
}
app.use(session(CONFIG,app));
3、设置和获取session
ctx.session.name = 'slj';
console.log(ctx.session.name) //slj