Linh Tran
Linh Tran

Reputation: 95

How to config Redis as session storage with KrakenJS

With Express, I can use Redis as session storage like this:

var express = require('express');
var app = express();
var RedisStore = require('connect-redis')(express);

app.use(express.cookieParser());
app.use(express.session({
    store: new RedisStore({
    host: 'localhost',
    port: 6379,
    db: 'test',
    pass: '123456'
  }),
  secret: '123456789'
}));

But with Kraken, the session configuration is in the file config/middleware.json. I changed the file to use connect-redis as following:

{
    "middleware": {
        "session": {
           "module": "connect-redis",
           "secret": "99b91c387e6e049308fc31d3cfff5fd3149e419c"
        }
    }
}

This way, Kraken does use Redis as session storage but how am I supposed to pass Redis' options (password, host, db name...) like with Express?

Upvotes: 4

Views: 1671

Answers (2)

Lenny Markus
Lenny Markus

Reputation: 3418

A complete working example can be found here: https://github.com/AlexSantos/Kraken_Example_Session_Redis

Upvotes: 2

Sudhir Kumar
Sudhir Kumar

Reputation: 126

The documentation for kraken-js is not very extensive. Hope it improves.

We had to dig into the code and understand the logic and we were able to solve.

Hope this help others who want to use Redis Session store with Kraken.js

Add "connect-redis" to package.json

"dependencies": {
    ...
    "connect-redis": ">=1.0.0"
},

Install "connect-redis" module

npm install

In the config/middleware.json add the config as below

{
    "middleware": {
        "session": {
            "module": "connect-redis",
            "config": {
                "host": "localhost",
                "port": 6379,
                "db": 1
            },
            "secret": "a1df0e81ef54d199567befb02761b1834c8b7406"
        }
    }
}

Change the config as required

Start/Restart server and it should work!

Provided you already have Redis running

Upvotes: 11

Related Questions