Reputation:
I am trying to setup Redis for a session store, but is not working at. I'm using passport.js
and express-flash
, and if I try run the current Redis setup, it won't work:
var session = require('express-session');
var favicon = require('serve-favicon');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var redis = require("redis").createClient();
var RedisStore = require("connect-redis")(session);
var load = require('express-load');
var flash = require('express-flash');
var path = require('path');
var logger = require('morgan');
var i18n = require('i18n-2');
var passport = require('passport');
var mongoose = require('mongoose');
If I use this session setup:
app.use(session({
secret: 'keyboard cat'
}));
This will show an error saying that is not safe for use in production, however the passport.js
and the express-flash will work.
Moving on to Redis:
app.use(session({
store: new RedisStore({
host: 54.94.171.197,
port: 3000,
client: redis
}),
secret: 'keyboard cat'
}));
Should I put the static IP in the host and the 3000 in the port? I am very confused with the proper values that I need pass to the new instance.
This is my middleware:
app.use(favicon(__dirname + '/public/images/icons/fav.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(flash());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
store: new RedisStore({
host: global.config.site.host,
port: 6379,
client: redis
}),
secret: 'keyboard cat'
}));
// Productions Middlewares
if (process.env.NODE_ENV === 'production') {
app.use(passport.initialize());
app.use(passport.session());
app.use('/admin', middleware.ensureAuthenticated);
app.use(middleware.ensureHttps);
}
Upvotes: 0
Views: 1585
Reputation: 22210
It’s bad practice to have your application’s configuration inside the code.
Use something like nconf to have your configuration out of the code.
For example you could use a config.json
file:
{
"sessionSecret": "cat something",
"redis": {
"host": "localhost",
"port": 6379
}
}
Configure nconf
to look for the configuration file
var nconf = require('nconf');
nconf.file({ file: '/path/to/config.json' })
Then use the configuration in your session middleware
app.use(session({
store: new RedisStore({
host: nconf.get('redis:host'),
port: nconf.get('redis:port'),
client: redis
}),
secret: nconf.get('sessionSecret')
}));
Upvotes: 1