Using Redis as session store for ExpressJS + PassportJS settings
Jun 26, 2014
Since all other articles about using Redis as a session store for ExpressJS are out of date due to latest update of connect-redis
, I’m going to write this short article to show how to use Redis as session store for ExpressJS.
In order to use Redis as session store for ExpressJS, first of all install connect-redis
package using npm
:
npm install connect-redis
Then, add connect-redis
to your dependencies and Following code shows the content of app.js
file:
var express = require('express');
var RedisStore = require('connect-redis')(express.session);
app.use(express.session({ store: new RedisStore({
host: '127.0.0.1',
port: 6379
}), secret: 'hey you' }));
If you’re using PassportJS
for users authentication, you can simply add two following lines to your app.js
file to enable PassportJS as well:
app.use(passport.initialize());
app.use(passport.session());
That’s it. Now you have PassportJS for authentication and Redis as session store for ExpressJS.
Furthermore you can use following code to delete the Redis session:
exports.logout = function (req, res) {
req.session.destroy();
res.redirect('/');
};