KoaHub平台基于Node.js开发的Koa的get/set session插件代码详情
koa-session2
Middleware for Koa2 to get/set session use with custom stores such as Redis or mongodb with Babel
koa-session2
Middleware for Koa2 to get/set session use with custom stores such as Redis or mongodb with Babel
If you are not using babel in your projects, maybe you can try this version without babel.
Install
npm install koa-session2
Usage
import Koa from "koa"; import session from "koa-session2"; const app = new Koa(); app.use(session({ key: "SESSIONID", //default "koa:sess" }));
Custom Stores
Store.js
import Redis from "ioredis"; import {Store} from "koa-session2"; export default class RedisStore extends Store { constructor() { super(); this.redis = new Redis(); } async get(sid) { return await this.redis.get(`SESSION:${sid}`); } async set(session, opts) { if(!opts.sid) { opts.sid = this.getID(24); } await this.redis.set(`SESSION:${opts.sid}`, session); return opts.sid; } async destroy(sid) { return await this.redis.del(`SESSION:${sid}`); } }
main.js
import Koa from "koa"; import session from "koa-session2"; import Store from "./Store.js"; const app = new Koa(); app.use(session({ store: new Store() })); app.use(ctx => { let user = ctx.session.user; ctx.session.view = "index"; });
Options
Most options based on cookies
key
: a string for store session id in cookiestore
: a class for custom store (extend {Store}, func: #get(sid), #set(session, opts), #destory(sid))maxAge
: a number representing the milliseconds fromDate.now()
for expiryexpires
: aDate
object indicating the cookie's expiration date (expires at the end of session by default).path
: a string indicating the path of the cookie (/
by default).domain
: a string indicating the domain of the cookie (no default).secure
: a boolean indicating whether the cookie is only to be sent over HTTPS (false
by default for HTTP,true
by default for HTTPS).httpOnly
: a boolean indicating whether the cookie is only to be sent over HTTP(S), and not made available to client JavaScript (true
by default).signed
: a boolean indicating whether the cookie is to be signed (false
by default). If this is true, another cookie of the same name with the.sig
suffix appended will also be sent, with a 27-byte url-safe base64 SHA1 value representing the hash of cookie-name=cookie-value against the first Keygrip key. This signature key is used to detect tampering the next time a cookie is received.overwrite
: a boolean indicating whether to overwrite previously set cookies of the same name (false
by default). If this is true, all cookies set during the same request with the same name (regardless of path or domain) are filtered out of the Set-Cookie header when setting this cookie.
License
MIT
代码地址:http://js.koahub.com/home/feature/koa-session2
KoaHub平台基于Node.js开发的Koa的get/set session插件代码详情的更多相关文章
- KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情
koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...
- KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情
KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...
- KoaHub平台基于Node.js开发的Koa的简单包装到请求库的类似接口
co-request co-request promisify wrapper for request co-request Simple wrapper to the request library ...
- KoaHub平台基于Node.js开发的Koa JWT认证插件代码信息详情
koa-jwt Koa JWT authentication middleware. koa-jwt Koa middleware that validates JSON Web Tokens and ...
- KoaHub平台基于Node.js开发的Koa的调试实用程序
debug small debugging utility debug tiny node.js debugging utility modelled after node core's debugg ...
- KoaHub平台基于Node.js开发的Koa的连接MongoDB插件代码详情
koa-mongo MongoDB middleware for koa, support connection pool. koa-mongo koa-mongo is a mongodb midd ...
- KoaHub平台基于Node.js开发的Koa的rewrite and index support插件代码详情
koa-static-server Static file serving middleware for koa with directory, rewrite and index support k ...
- KoaHub平台基于Node.js开发的Koa的skip插件代码详情
koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...
- KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情
koa-router Router middleware for koa. Provides RESTful resource routing. koa-router Router mid ...
随机推荐
- oracle存储过程中文乱码问题
设置环境变量,新建变量,设置变量名:NLS_LANG,变量值:SIMPLIFIED CHINESE_CHINA.ZHS16GBK word哥,还是不行呀: 参考:http://idata.blog.5 ...
- 扩展BaseAdapter实现不存储列表项的ListView
下面的实例将会通过扩展BaseAdapter来实现Adapter,扩展BaseAdapter可以取得对Adapter最大的控制权:程序要创建多个列表项,每个列表项的组件都由开发者来决定. 下面的布局文 ...
- ImageView及其子类(三)
实例:使用QuickContactBadge关联联系人 QuickContactBadge继承了ImageView,因此它的本质也是图片,也可以通过android:src属性指定他显示的图片 ...
- Flex中escape/encodeURI/encodeURIComponent的区别
Flex中提供了三种转码函数,各有各的区别, escape,encodeURI,encodeURIComponent 这三个函数不仅在flex中有道运用在javascript中同样的含义 ,今天我仔细 ...
- C++中的RAII技法
Resource Acquisition Is Initialization or RAII, is a C++ programming technique which binds the life ...
- iOS tabbar点击动画效果实现
正常情况下,我们点击tabbar都只有一个变色效果,但有时候,如果我们想给它添加一个点击动画,该如何做呢? 先上几个效果图: 1.先放大,再缩小 2.Z轴旋转 3.Y轴位移 ...
- Bootstrap入门(十三)组件7:导航条
Bootstrap入门(十三)组件7:导航条 1.默认样式的导航条 2.嵌入表单和按钮 3.嵌入文本和非导航的链接 4.组件排列和下拉菜单 5.固定在顶部/底部 6.反色的导航条 7.路径导航 首先先 ...
- 让EFCore更疯狂些的扩展类库(一):通过json文件配置sql语句
前言 EF通过linq和各种扩展方法,再加上实体模型,编写数据库的访问代码确实是优美.舒服,但是生成的sql不尽如意.性能低下,尤其是复杂些的逻辑关系,最终大家还是会回归自然,选择能够友好执行sql语 ...
- python enhanced generator - coroutine
本文主要介绍python中Enhanced generator即coroutine相关内容,包括基本语法.使用场景.注意事项,以及与其他语言协程实现的异同. enhanced generator 在上 ...
- DevExpress控件之RepositoryItemComboBox
RepositoryItemComboBox在嵌入到GridView后,如何获取当前所选的Item? 直接代码: ((RepositoryItemComboBox)gridView.Columns[& ...