C/C++教程

session和cookie

本文主要是介绍session和cookie,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
const Koa = require('koa') const router = require('koa-router')() const views = require('koa-views') const nunjucks = require('nunjucks') const session = require('koa-session')
const app = new Koa() app.keys = ['123456']
app.use(   session(     {       maxAge: 10*1000     },     app   ) ) app.use(   views(__dirname + '/views', {     map: { html: 'nunjucks' }   }) )
//cookie router.get('/', ctx => {   ctx.cookies.set('user', 'username')   ctx.body = 'cookie' }) router.get('/test', ctx => {   let count = ctx.cookies.get('count') //获取cookie   if (count > 0) {     count++     ctx.cookies.set('count', count, {       maxAge: 2000 //设置cookie过期时间     })   } else {     count = 1     ctx.cookies.set('count', count)   }   ctx.body = count }) //session router.get('/session', async ctx => {   ctx.session.user = 'admin'   console.log(ctx.session.user)   ctx.body = 'set session' }) router.get('/sessionq', async ctx => {   let user = ctx.session.user   console.log(user)   ctx.body = user })


app.use(router.routes()) app.listen(3001, () => {   console.log('server is running') })
这篇关于session和cookie的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!