koa提供了從上下文直接讀取屁桑、寫入cookie的方法
ctx.cookies.get(name, [options]) 讀取上下文請(qǐng)求中的cookie
ctx.cookies.set(name, value, [options]) 在上下文中寫入cookie
const Koa = require('koa')
const config = require('../config')
const app = new Koa()
app.use(async(ctx)=>{
if(ctx.url==='/index'){
ctx.cookies.set('cid','comedy',{
domain:'localhost', //寫cookie所在的域名
path:'/index', //寫cookie所在的路徑
maxAge:60*1000, //寫cookie有效時(shí)長
expires:7,
httpOnly:false,
overwrite:false
})
ctx.body = 'cookie is ok'
}else{
ctx.body = 'hello world'
}
})
app.listen( config.port ,()=>{console.log(`端口號(hào)為${config.port}的node項(xiàng)目啟動(dòng)成功`);})