1、安装
cnpm install koa-session --save
2、引入
const session=require('koa-session');
3、配置session,在路由启动之前
app.keys = ['some secret hurr'];
const CONFIG = {
key: 'koa.sess',
maxAge: 86400000,
autoCommit: true, /** (boolean) automatically commit headers (default true) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
// httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
rolling: true, /** 每次请求重新设置时间*/
renew: false, /** 请求时在快要过期时重新设置cookie*/
sameSite: null, /** (string) session cookie sameSite options (default null, don't set it) */
};
app.use(session(CONFIG, app));
4、设置session
ctx.session.键名=值;
5、读取session
ctx.session.键名;
6、清楚session
ctx.session=null;
代码示例:
const Koa=require('koa');
const Router=require('koa-router');
// const views=require('koa-views');
const commonm=require('./public/common');
const bodyParser=require('koa-bodyparser');
const static=require('koa-static');
const render = require('koa-art-template');
const path=require('path');
const session=require('koa-session');
const app=new Koa();
const router=new Router();
app.keys = ['some secret hurr'];
const CONFIG = {
key: 'koa.sess', /** (string) cookie key (default is koa.sess) */
/** (number || 'session') maxAge in ms (default is 1 days) */
/** 'session' will result in a cookie that expires when session/browser is closed */
/** Warning: If a session cookie is stolen, this cookie will never expire */
maxAge: 86400000,
autoCommit: true, /** (boolean) automatically commit headers (default true) */
overwrite: true, /** (boolean) can overwrite or not (default true) */
// httpOnly: true, /** (boolean) httpOnly or not (default true) */
signed: true, /** (boolean) signed or not (default true) */
rolling: true, /** 每次请求重新设置时间*/
renew: false, /** 请求时在快要过期时重新设置cookie*/
sameSite: null, /** (string) session cookie sameSite options (default null, don't set it) */
};
app.use(session(CONFIG, app));
//配置模板引擎
// app.use(views('views',{extension:"ejs" })); //文件后缀必须为.ejs
// app.use(views('views',{map:{html:'ejs'}}));
render(app, {
root: path.join(__dirname, 'views'), //配置模板引擎路径
extname: '.html', //以什么后缀结尾
debug: process.env.NODE_ENV !== 'production' //在开发阶段开启debug
});
//配置static
app.use(static('static'));
app.use(router.routes())
.use(router.allowedMethods());
app.use(bodyParser());
router.get('/',async(ctx,next)=>{
await ctx.render("index",{
title:'jeff',
info:"<h2 style='color:red'>渲染</h2>"
});
console.log(ctx.session.uname);
})
router.get('/news',async(ctx,next)=>{
await ctx.render('news',{
names:"jeff"
})
ctx.session.uname='jeff';
})
//路由中间件
router.get('/det',async(ctx,next)=>{
// ctx.body='det1';
console.log('aaa');
await next();
})
router.get('/det',async(ctx,next)=>{
ctx.body='det2';
})
router.post('/info',async (ctx,next)=>{
// var data=await commonm.Posts(ctx);
var data=ctx.request.body.name;
ctx.body=data;
})
router.get('/msg',async(ctx,next)=>{
ctx.body=ctx.query.id;
})
//动态路由
router.get('/home/:aid/:aic',async(ctx,next)=>{
ctx.body=ctx.params.aid+ctx.params.aic;
})
app.listen(3000,()=>{
console.log('this koa server is running at http://localhost:3000/');
})