koa2--06.cookies的设置和使用
cookies本身在koa中即可直接设置和使用,不需要在安装中间件
//cookies的使用 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 app = new koa(); app.use(views(‘views‘,{ map:{ html:‘ejs‘ } })); app.use(bodyParser()); app.use(static(‘./www‘)); router(‘/‘,async(ctx)=>{ ctx.cookies.set(‘id‘,‘123‘,{maxAge:1000});//设置cookie值,第一个参数为name,第二个为values,第三个为可选选项 id = ctx.cookies.get(‘id‘); }); console.log(new Buffer(‘hello, world!‘).toString(‘base64‘));// 转换成 base64 字符 串:aGVsbG8sIHdvcmxkIQ== console.log(new Buffer(‘aGVsbG8sIHdvcmxkIQ==‘, ‘base64‘).toString());// 还原 base 64 字符串:hello, world! app.use(router.routes()); app.use(router.allowedMethods()); app.listen(80);
相关推荐
webgm 2020-08-16
LorenLiu 2019-11-04
oSweetBear 2019-11-04
lei0 2020-11-02
igogo00 2020-07-04
iconhot 2020-06-26
88481456 2020-06-18
疯狂紫萧 2020-06-16
yezitoo 2020-06-06
SelinaChan 2020-05-15
luckymaoyy 2020-04-25
80437700 2020-04-17
austindev 2020-04-07
我有一只小松鼠 2020-02-29
webfullStack 2020-02-15
binglingnew 2012-10-03
始作俑者 2020-01-19
jinxiutong 2020-01-04