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

KoaHub.js -- 基于 Koa.js 平台的 Node.js web 快速开发框架

官网:http://js.koahub.com

基于 Koa平台Node.js开发的KoaHub.js获取/设置会话功能代码的更多相关文章

  1. 基于 Koa平台Node.js开发的KoaHub.js的控制器,模型,帮助方法自动加载

    koahub-loader koahub-loader是基于 Koa平台Node.js开发的KoaHub.js的koahub-loader控制器,模型,帮助方法自动加载 koahub loader I ...

  2. 基于 Koa平台Node.js开发的KoaHub.js连接打印机的代码

    最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易 ...

  3. 基于 Koa平台Node.js开发的KoaHub.js的静态服务器重写和索引代码

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

  4. 基于 Koa平台Node.js开发的KoaHub.js的模板引擎代码

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

  5. 基于 Koa平台Node.js开发的KoaHub.js的输出json到页面代码

    koahub-body-res koahub body res Format koa's respond json. Installation $ npm install koahub-body-re ...

  6. 基于 Koa平台Node.js开发的KoaHub.js的跳过组件代码

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

  7. 基于LBS平台的iOS开发

    LBS,即Location Based Services,基于位置服务,用于定位.导航等功能,比如地图应用.订外卖等的app就需要这个功能. 在这里我使用的是高德LBS开放平台,地址:http://l ...

  8. 基于CkEditor实现.net在线开发之路(2)编写C#代码,怎么调用它。

    上一章简约的介绍了CkEditor编辑器,可以编辑js逻辑代码,css,html,C#代码,这章我根据实际例子,讲解怎么编写C#代码和怎么调用它. 大家都还记得刚刚接触程序编时的hello Word吧 ...

  9. 手机开发必备技巧:javascript及CSS功能代码分享

    1. viewport: 也就是可视区域.对于桌面浏览器,我们都很清楚viewport是什么,就是出去了所有工具栏.状态栏.滚动条等等之后用于看网页的区域,这是真正有效的区域.由于移动设备屏幕宽度不同 ...

随机推荐

  1. UED大全

    http://www.baiduux.com/  百度UFOhttp://ued.sohu.com/  搜狐UEDhttp://ued.taobao.com/  淘宝UEDhttp://www.ued ...

  2. jQuery表单对象属性过滤选择器

    jQuery表单对象属性过滤选择器 <div id="p1" attr="p1"> <input type="text" ...

  3. Nancy简单实战之NancyMusicStore(二):打造首页

    前言 继上一篇搭建好项目之后,我们在这一篇中将把我们NancyMusicStore的首页打造出来. 布局 开始首页之前,我们要先为我们的整个应用添加一个通用的布局页面,WebForm中母版页的概念. ...

  4. 转:Spring FactoryBean源码浅析

    http://blog.csdn.net/java2000_wl/article/details/7410714 在Spring BeanFactory容器中管理两种bean 1.标准Java Bea ...

  5. PHP 7.1 新特性

    PHP 7.1 新特性 1.密集阵算法 2.php int64位支持(2GB的字符串和2GB的文件的上传) 3.$a<=>$b  操作符,排序时有用 4.标量的支持,如果声明int传入st ...

  6. 使用vlmcsd自建KMS服务~一句命令激活windows/office

    服务作用:在线激活windows和office 适用对象:VOL版本的windows和office 适用版本:截止到win10和office2016的所有版本 服务时间:24H,偶尔更新维护 优点:在 ...

  7. (五)Lua脚本语言入门

    ---恢复内容开始--- 写完这篇Lua脚本语言入门,自己就要尝试去用Lua脚本语言写esp8266了,,自己现在挺心急的,因为朋友使用esp8266本来说自己帮忙写好程序的,但是用的单片机不一样自己 ...

  8. FZU 1889 龟兔赛跑

    Problem 1889 龟兔赛跑 Accept: 1240    Submit: 1650Time Limit: 1000 mSec    Memory Limit : 32768 KB Probl ...

  9. 深度神经网络(DNN)反向传播算法(BP)

    在深度神经网络(DNN)模型与前向传播算法中,我们对DNN的模型和前向传播算法做了总结,这里我们更进一步,对DNN的反向传播算法(Back Propagation,BP)做一个总结. 1. DNN反向 ...

  10. 读书笔记 effective c++ Item 18 使接口容易被正确使用,不容易被误用

    1. 什么样的接口才是好的接口 C++中充斥着接口:函数接口,类接口,模板接口.每个接口都是客户同你的代码进行交互的一种方法.假设你正在面对的是一些“讲道理”的人员,这些客户尝试把工作做好,他们希望能 ...