Вот что у меня получилось в итоге:
файл app.js:
"use strict";
// Include modules
var express = require("express"),
path = require("path"),
cookieParser = require("cookie-parser"),
bodyParser = require("body-parser"),
session = require("express-session"),
RedisStore = require("connect-redis")(session),
Redis = require('ioredis'),
config = require("./config"),
errorMessage = require("./errors/errorMessage"),
sessionMethods = require("./utils/sessionMethods"),
helmet = require("helmet"),
middleware = require("./routes/middleware"),
app = express(),
session = createAppSpecificSession(config),
function createAppSpecificSession(config) {
var redis = null;
redis = new Redis({
host: config.SESSION.host,
port: config.SESSION.port,
password: config.SESSION.password
});
return session({
store: new RedisStore({
client: redis
}),
resave: false,
saveUninitialized: false,
secret: config.SESSION.cookieSecret,
name: config.SESSION.cookieName,
genid: sessionMethods.generateSessionId
});
}
app.use(helmet());
app.use(middleware.setUUIDOnReq);
app.use(middleware.setAgentOnReq);
// additional app modules
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cookieParser());
app.use("/", session);
// Include routes
require("./routes/app")(express, app);
module.exports = app;
И файл конфигурации:
config.js
module.exports = {
SESSION: {
hosts: [{
host: "endpoint1.0001.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}, {
host: "endpoint2.0002.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}, {
host: "endpoint3.0003.apse1.cache.amazonaws.com",
port: 6379,
password: "",
}],
cookieName: "connect.sid",
cookieSecret: ["blablabla", "blablabla"]
}
};