- index-cookie.js(test3)中原有代码如下
// Koa2操作cookie
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
ctx.body = 'cookie test by koa2'
})
app.listen(3000)
- 设置cookie
app.use(async (ctx) => {
ctx.body = 'cookie test by koa2'
// 设置cookie
ctx.cookies.set('a', '100')
})
3. 获取cookie
app.use(async (ctx) => {
ctx.body = 'cookie test by koa2'
// 设置cookie
ctx.cookies.set('a', '100')
// 获取cookie(结构化koa2已经做好)
console.log('cookie is', ctx.cookies.get('a'))
})
4. 完整代码:
// Koa2操作cookie
const Koa = require('koa');
const app = new Koa();
app.use(async (ctx) => {
ctx.body = 'cookie test by koa2'
// 设置cookie
ctx.cookies.set('a', '100')
// 获取cookie(结构化koa2已经做好)
console.log('cookie is', ctx.cookies.get('a'))
})
app.listen(3000)