Koa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 var koa = require ('koa' );var app = new koa();var router = require ('koa-router' )(); router.get('/' ,async (ctx)=>{ ctx.body = '首页' ; }).get('/news' ,async (ctx)=>{ console .log(ctx.query); console .log(ctx); ctx.body = '这是新闻页面' ; }) .get('/newscontent/:aid' ,async (ctx)=>{ console .log(ctx.params) ctx.body = '新闻详情' }) app .use(router.routes()) .use(router.allowedMethods()); app.listen(3000 ); console .log("http://127.0.0.1:3000" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 var koa = require ('koa' );var app = new koa();var router = require ('koa-router' )();app.use(async (ctx,next)=>{ console .log("这是一个中间件" ); await next(); if (ctx.status == 404 ){ ctx.body = '这是一个404页面' }else { console .log(ctx.url); } }) router.get('/' ,async (ctx)=>{ ctx.body = '首页' ; }).get('/news' ,async (ctx)=>{ console .log('这是一个新闻页面' ) ctx.body = '这是新闻页面' ; }) .get('/newscontent/:aid' ,async (ctx)=>{ console .log(ctx.params) ctx.body = '新闻详情' }) app .use(router.routes()) .use(router.allowedMethods()); app.listen(3000 ); console .log("http://127.0.0.1:3000" )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 var koa = require ('koa' );var app = new koa();var router = require ('koa-router' )();var views = require ('koa-views' );var bodyParser = require ('koa-bodyparser' );const static = require ('koa-static' );app.use(views('views' ,{ extension:'ejs' })) app.use(bodyParser()) app.use(static ('./static' )); router.get('/' ,async (ctx)=>{ let title = "你好,ejs" ; await ctx.render('index' ,{ title:title }) }) .get('/news' ,async (ctx)=>{ console .log('这是一个新闻页面' ) ctx.body = '这是新闻页面' ; }) .get('/newscontent/:aid' ,async (ctx)=>{ console .log(ctx.params) ctx.body = '新闻详情' }) router.post('/doAdd' ,async (ctx)=>{ console .log(ctx.request.body); ctx.body = ctx.request.body; }) app .use(router.routes()) .use(router.allowedMethods()); app.listen(3000 ); console .log("http://127.0.0.1:3000" )
koa-session session简介:
session是另一种记录客户状态的机制,不同的是Cookie保存在客户端保存在浏览器中,而session保存在服务器上。
session工作流程:
当浏览器访问服务器并发送第一次请求时,服务器就会创建一个session对象,生成一个类似于key,value键值对,然后将key(cookie)返回到客户端,浏览器再次访问的时候,携带key(cookie),找到对应的session(value)。客户的信息都保存再session中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 const koa = require ('koa' );const app = new koa();const router = require ('koa-router' )();const views = require ('koa-views' );const bodyParser = require ('koa-bodyparser' );const static = require ('koa-static' );const session = require ('koa-session' );app.use(views('views' ,{ extension:'ejs' })) app.use(bodyParser()) app.use(static ('./static' )); app.keys = ['some secret hurr' ]; const CONFIG = { key: 'koa.sess' , maxAge: 86400000 , autoCommit: true , overwrite: true , httpOnly: true , signed: true , rolling: false , renew: true , sameSite: null , }; app.use(session(CONFIG, app)); router.get('/' ,async (ctx)=>{ console .log(ctx.session.userinfo) let title = "你好,ejs" ; await ctx.render('index' ,{ title:title }) }) .get('/news' ,async (ctx)=>{ ctx.body = '这是新闻页面' ; }) .get('/newscontent/:aid' ,async (ctx)=>{ console .log(ctx.params) ctx.body = '新闻详情' }) .get('/login' ,async (ctx)=>{ ctx.session.userinfo = '张三' ctx.body = "登录成功" }) router.post('/doAdd' ,async (ctx)=>{ console .log(ctx.request.body); ctx.body = ctx.request.body; }) app .use(router.routes()) .use(router.allowedMethods()); app.listen(3000 ); console .log("http://127.0.0.1:3000" )