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: aDateobject 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 (falseby default for HTTP,trueby 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 (trueby default).signed: a boolean indicating whether the cookie is to be signed (falseby default). If this is true, another cookie of the same name with the.sigsuffix 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 (falseby 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 表空间迁移
迁移表空间databump 使用databump导入导出,两个库用户必须一致,否则另一个库导入的时候会报错.所以两个库都是用helei用户. 给两个数据库的用户分别授予dba权限,这里只是实验更清晰而 ...
- [CSS3] 学习笔记-CSS3盒子样式
1.盒子的类型 在CSS3中,使用display来定义盒子的类型,包括block,inline,inline-block类型.div元素和P元素,属于block类型,span元素和a元素,属于inli ...
- Android音频焦点详解(上)
转载请注明出处:http://www.cnblogs.com/landptf/p/6384112.html 2017年开年第一篇博客,很早就想总结一下Android音频的相关知识.今天我们先来看一下音 ...
- bzoj1492--斜率优化DP+cdq分治
显然在某一天要么花完所有钱,要么不花钱. 所以首先想到O(n^2)DP: f[i]=max{f[i-1],(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j])},j& ...
- 如何一步一步用DDD设计一个电商网站(十四)—— 回顾与总结
本系列所有文章 如何一步一步用DDD设计一个电商网站(一)—— 先理解核心概念 如何一步一步用DDD设计一个电商网站(二)—— 项目架构 如何一步一步用DDD设计一个电商网站(三)—— 初涉核心域 如 ...
- [个人翻译]Redis 集群教程(下)
[个人翻译]Redis 集群教程(上) [个人翻译]Redis 集群教程(中) 官方原文地址:https://redis.io/topics/cluster-tutorial 水平有限,如果您在阅读过 ...
- 用《内网穿山甲》把本地IIS中的站点共享到远程访问
前言: 因为各种原因,我们常常要把本机或局域网中搭建的站点发给远方的人访问,他有可能是测试人员.客户.前端.或领导演示,或是内部系统内部论坛临时需要在远程访问,事件变得很麻烦,要么有公网IP,要么能控 ...
- Java模拟新浪微博登陆抓取数据
前言: 兄弟们来了来了,最近有人在问如何模拟新浪微博登陆抓取数据,我听后默默地抽了一口老烟,暗暗的对自己说,老汉是时候该你出场了,所以今天有时间就整理整理,浅谈一二. 首先: 要想登陆新浪微博需要 ...
- SQL极限函数limit()详解<分页必备>
limit含义: limit英语中的含义是限制,限定的意思.小日本曾上映过一个电影就是叫limit是由漫画改编的电影,剧情很变态,但不可否认小日本由于地狭人稠的原因,在观念上的资源危机意识还是很强的哈 ...
- 【轮子狂魔】手把手教你用JS给博客动态增加目录 - 超级懒人版
动态显示目录的作用 不用每次写博客的时候繁琐的人工整理目录,又可以动态浮动在右下角,方便快速跳到感兴趣的位置同时也可以快速的对文章内容有一个大概的了解. 实现原理 首先根据个人喜好,我习惯了用 h1 ...