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 cookie

  • store: a class for custom store (extend {Store}, func: #get(sid), #set(session, opts), #destory(sid))

  • maxAge: a number representing the milliseconds from Date.now() for expiry

  • expires: a Date 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 .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 (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

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统
wemall地址:http://www.wemallshop.com
代码地址:http://js.koahub.com/home/feature/koa-session2

 
 

KoaHub平台基于Node.js开发的Koa的get/set session插件代码详情的更多相关文章

  1. KoaHub平台基于Node.js开发的Koa的模板系统handlebars插件代码详情

    koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-ha ...

  2. KoaHub平台基于Node.js开发的Koa 连接支付宝插件代码信息详情

    KoaHub平台基于Node.js开发的Koa 链接支付宝插件代码信息详情 easy-alipay alipay payment & notification APIs easy-alipay ...

  3. KoaHub平台基于Node.js开发的Koa的简单包装到请求库的类似接口

    co-request co-request promisify wrapper for request co-request Simple wrapper to the request library ...

  4. KoaHub平台基于Node.js开发的Koa JWT认证插件代码信息详情

    koa-jwt Koa JWT authentication middleware. koa-jwt Koa middleware that validates JSON Web Tokens and ...

  5. KoaHub平台基于Node.js开发的Koa的调试实用程序

    debug small debugging utility debug tiny node.js debugging utility modelled after node core's debugg ...

  6. KoaHub平台基于Node.js开发的Koa的连接MongoDB插件代码详情

    koa-mongo MongoDB middleware for koa, support connection pool. koa-mongo koa-mongo is a mongodb midd ...

  7. KoaHub平台基于Node.js开发的Koa的rewrite and index support插件代码详情

    koa-static-server Static file serving middleware for koa with directory, rewrite and index support k ...

  8. KoaHub平台基于Node.js开发的Koa的skip插件代码详情

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  9. KoaHub平台基于Node.js开发的Koa router路由插件代码信息详情

    koa-router Router middleware for koa. Provides RESTful resource routing. koa-router       Router mid ...

随机推荐

  1. ThinkPHP 分组,应用,跳转

    一.多应用配置技巧    在主入口文件index.php同级目录,新建一个 config.php 写入公共的配置项,然后在前后台各自的配置文件config.php中    $arr = include ...

  2. java程序的工作原理

    Sun公司设计java语言的目标是让Java程序不必经过修改就可以在各种各样的计算机(包括PC机和工作站)上运行.为了实现这一目标,Sun公司提供了一阵Java虚拟机(Java Virtual Mac ...

  3. 如何快速定位到Eclipse自动添加的TODO

    把自动生成的// TODO ....前面加上todo,这样生成之后就会有编译错误,直接 ctrl+. 就到该位置了,可以删除todo留着// TODO ...,也可以ctrl+d删除一行: 不建议不生 ...

  4. 第一篇:CUDA 6.0 安装及配置( WIN7 64位 / 英伟达G卡 / VS2010 )

    前言 本文讲解如何在VS 2010开发平台中搭建CUDA开发环境. 当前配置: 系统:WIN7 64位 开发平台:VS 2010 显卡:英伟达G卡 CUDA版本:6.0 若配置不同,请谨慎参考本文. ...

  5. 【js 编程艺术】小制作三

    1.html文件 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  6. MEAN教程2-Nodejs安装

    安装Node.js稳定版本最简单的办法也是使用二进制文件,Node.js官方网站上提供了下载地址,可用于Linux.Mac OS X和Windows系统.同样要注意下载与目标操作系统架构一致的文件. ...

  7. Hibernate核心配置文件

    Hibernate.cfg.xml是Hibernate操作数据库的核心配置文件 *********************************************** 作用 01.管理实体类的 ...

  8. FindPkgConfig----CMake的pkg-config模块

    FindPkgConfig A pkg-config module for CMake. CMake的pkg-config模块. Finds the pkg-config executable and ...

  9. springmvc注解式事务手动回滚

    Spring的AOP事务管理默认是针对unchecked exception回滚(运行期异常,Runtime Exception),如果希望手动控制事务的回滚,可以通过 TransactionAspe ...

  10. (3)activiti流程的挂起和激活

    有时候,我们需要对一个已经执行的流程进行暂停,而不是删除它,这个时候就需要我们调用activiti暂停和激活的api来操作他们 每启动一个流程实例,都会在该流程实例下产生相应的流程任务,处于1*多的关 ...