nodejs session

本文介绍了一个基于Koa2的中间件koa-session2,用于处理会话的获取与设置。该中间件支持自定义存储,例如使用Redis或MongoDB,并提供了如何配置和使用的详细示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

独家直播!大数据应用场景全解析>>>   hot3.png

0

收藏(0)

 

 

 

 

 

 

 

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

 

  1. import Koa from "koa";
  2. import session from "koa-session2";
  3.  
  4. const app = new Koa();
  5.  
  6. app.use(session({
  7.     key: "SESSIONID",   //default "koa:sess" 
  8. }));

 

Custom Stores

Store.js

 

  1. import Redis from "ioredis";
  2. import {Store} from "koa-session2";
  3.  
  4. export default class RedisStore extends Store {
  5.     constructor() {
  6.         super();
  7.         this.redis = new Redis();
  8.     }
  9.  
  10.     async get(sid) {
  11.         return await this.redis.get(`SESSION:${sid}`);
  12.     }
  13.  
  14.     async set(session, opts) {
  15.         if(!opts.sid) {
  16.             opts.sid = this.getID(24);
  17.         }
  18.         await this.redis.set(`SESSION:${opts.sid}`, session);
  19.         return opts.sid;
  20.     }
  21.  
  22.     async destroy(sid) {
  23.         return await this.redis.del(`SESSION:${sid}`);
  24.     }
  25. }

 

main.js

 

  1. import Koa from "koa";
  2. import session from "koa-session2";
  3. import Store from "./Store.js";
  4.  
  5. const app = new Koa();
  6.  
  7. app.use(session({
  8.     store: new Store()
  9. }));
  10.  
  11. app.use(ctx => {
  12.     let user = ctx.session.user;
  13.  
  14.     ctx.session.view = "index";
  15. });
  16.  

 

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

转载于:https://my.oschina.net/u/2603728/blog/857896

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值