ExpressJS and remote redis session storage

I wanted to write this article as I couldn't find any good information on connecting ExpressJS, Remote Redis and Sessions.

Require'd

var session = require('express-session')
var uuid = require('node-uuid');
var redisStore = require('connect-redis')(session);

I wanted a good library to generate uuid's to use as my session keys so I decided on using node-uuid.

Config

app.use(session({
    genid: function(req) {
        return uuid.v4();
    },
    secret: 'zAc2Y6Y7g3+7M28fJ573f_5Ku~.%4G2f%897w4tH~T~9DZAz7+.54G_|=.-kX3NE',
    resave: false,
    saveUninitialized: true,
    store: new redisStore({
        host : '192.168.0.83',
        port : '6379',
        pass : 'abc123'
    }),
}));

We define the provider for our UUID, set a secret [64 characters for me], and setup the session storage.

Please remember that the storage is by default in the memory of the node app. In my planned infrastructure I want to use a single Redis server with one or more application servers behind a load balancers. This way I do not have to worry about sticky sessions or any jazz.

Simple. Done.

Written by Glenn Eggleton on Wednesday February 24, 2016
Permalink - Chapter: Node.JS