LorenLiu 2019-11-04
首先安装 koa-session中间件
//koa-session的使用 const koa = require(‘koa‘); var router = require(‘koa-router‘)(); var views = require(‘koa-views‘); const static = require(‘koa-static‘); const bodyParser = require(‘koa-bodyparser‘); var session = require(‘koa-session‘); var app = new koa(); app.use(views(‘views‘,{ map:{ html:‘ejs‘ } })); app.use(bodyParser()); app.use(static(‘./www‘)); //设置签名 var keys = []; ()=>{ for(var i =0;i<1000;i++){ keys.push(Math.random()*100); } }; app.keys = keys; const CONFIG = { keys:‘koa:sess‘, maxAge:1000 }; app.use(session(CONFIG,app));//使用session app.use(router.routes()); app.use(router.allowedMethods()); app.listen(80);