基于 Koa平台Node.js开发的KoaHub.js获取/设置会话功能代码

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

时间: 2024-10-20 17:25:20

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

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

koahub-loader koahub-loader是基于 Koa平台Node.js开发的KoaHub.js的koahub-loader控制器,模型,帮助方法自动加载 koahub loader Installation $ npm install koahub-loader Use with koa // 1.model loader var model = loader([ {      root: './app/model',      suffix: '.model.js' }, { 

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

最近好多小伙伴都在做微信商城的项目,那就给大家分享一个基于 Koa.js 平台的 Node.js web 开发的框架连接微信易联云打印机接口的代码,供大家学习.koahub-yilianyun 微信易联云打印机接口 koahub-yilianyun易联云打印机node接口 Installation $ npm install koahub-yilianyunUse with co-request var printer = require('koahub-yilianyun');var resu

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

koa-static-server Static file serving middleware for koa with directory, rewrite and index support koa-static-server static file serving middleware for koa with directory, rewrite and index support Installation $ npm install koa-static-server API var

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

koahub-handlebars koahub-handlebars koahub handlebars templates Installation $ npm install koahub-handlebars Use with koa var koa = require('koa'); var hbs = require('koahub-handlebars'); var app = koa(); // koahub-handlebars is middleware. `use` it 

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

koahub-body-res koahub body res Format koa's respond json. Installation $ npm install koahub-body-res Use with koa var app = require('koa')(); var koaRes = require('koahub-body-res'); app.use(koaRes()); this.data = 'This is a body'; this.msg = 'This 

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

koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is met. Install npm i koahub-skip --save Usage With existing middlewares: var skip = require('koahub-skip'); var serve  = require('koa-static'); var stati

[Tizen开发]基于Tizen平台的终端开发模拟器1 - Web

1.  官方 模拟器 Tutorials https://01.org/web-simulator/ 2. 最新版本发布于 2013.05 I am honored to introduce the Web Simulator for its public release on 01.org. The Web Simulator is a lightweight mobile application development and testing tool for web application

基于LBS平台的iOS开发

LBS,即Location Based Services,基于位置服务,用于定位.导航等功能,比如地图应用.订外卖等的app就需要这个功能. 在这里我使用的是高德LBS开放平台,地址:http://lbs.amap.com/ 进入网站,首先注册并认证为开发者,然后为你的每个APP申请一个key,其中安全码(Bundle Identifier)通过Xcode切换到General标签,查看Bundle Identifier. 使用第三方服务,我们可以使用自动配置,这里就要使用到Cocoapods.C

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

上一章简约的介绍了CkEditor编辑器,可以编辑js逻辑代码,css,html,C#代码,这章我根据实际例子,讲解怎么编写C#代码和怎么调用它. 大家都还记得刚刚接触程序编时的hello Word吧,首先我来介绍在编辑器上写了返回“hello Word”的C#方法,让后怎么去调用返回对应hello Wrod方法,具体代码如下: 运行预览效果如下图 页面加载之后就弹出了hello world对话框.证明获取到了我们写返回字符串. 注意编辑C#代码 并可以用ajax调用,C#代码方法必须要有一定的