session配置
app.keys = [‘UIsdf_7878#$’]
app.use(
session({
key: ‘weibo.sid’, // cookie name 默认koa.sid
prefix: ‘weibo:sess:’, //redis key的前缀
cookie: {
path: ‘/’,
httpOnly: true,
maxAge: 24 * 60 * 60 * 1000
},
store: redisStore({
all: ‘192.168.56.11:6379’
})
})
)
接口配置
router.get(’/json’, async (ctx, next) => {
const session = ctx.session
if (session.viewNum == null) {
session.viewNum = 0
}
session.viewNum++
ctx.body = {
title: ‘koa2 json’,
viewNum: session.viewNum
}
})
访问接口报错