koa-session-redis-store
Version:
redis store for koa-session
96 lines (67 loc) • 1.81 kB
Markdown
This is a redis store module for [koa-session](https://www.npmjs.com/package/koa-session).
It can be used like this:
```js
const session = require('koa-session');
const RedisStore = require('koa-session-redis-store');
const Koa = require('koa');
const app = new Koa();
app.keys = ['some secret hurr'];
const CONFIG = {
store: new RedisStore(),
// The others options for koa-session
};
app.use(session(CONFIG, app));
app.use(ctx => {
// ignore favicon
if (ctx.path === '/favicon.ico') return;
let n = ctx.session.views || 0;
ctx.session.views = ++n;
ctx.body = n + ' views';
});
app.listen(3000);
```
```shell
$ npm install koa-session-redis-store
```
> **Constructor**
```js
const redisStore = new RedisStore(opts);
```
**opts**: The [options](https://github.com/NodeRedis/node_redis#options-object-properties) that will be passed to redis client([node_redis](https://www.npmjs.com/package/redis)).
> **Events**
It could catch the [events](https://www.npmjs.com/package/redis#connection-and-other-events) from where redis client.
```js
redisStore.on('ready', function() {
});
redisStore.on('connect', function() {
});
redisStore.on('reconnecting', function(reconnectParams) {
});
redisStore.on('error', function(err) {
});
redisStore.on('end', function() {
});
redisStore.on('warning', function(msg) {
});
```
> **Close connection**
Close function is executed as asynchrous by using [bluebird](http://bluebirdjs.com/).
```js
redisStore.close()
.delay(1000) // It allows to use the bluebird api
.then(function(result) {
console.log(result);
});
```
or also can be called using async/await.
```js
const close = async () => {
const result = await redisStore.close();
console.log(result);
};
```
MIT