Skip to content

Commit

Permalink
Adds Redis support for session cache
Browse files Browse the repository at this point in the history
  • Loading branch information
benc-uk committed Sep 13, 2022
1 parent 2f11bdc commit a633a37
Show file tree
Hide file tree
Showing 3 changed files with 195 additions and 18 deletions.
172 changes: 165 additions & 7 deletions src/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nodejs-demoapp",
"description": "Node.js Express app for demos",
"version": "4.9.5",
"version": "4.9.6",
"author": "Ben Coleman",
"engines": {
"node": ">=16.0.0"
Expand All @@ -26,14 +26,16 @@
"@azure/msal-node": "^1.14.0",
"applicationinsights": "^2.3.4",
"axios": "^0.27.2",
"connect-redis": "^6.1.3",
"cookie-parser": "^1.4.6",
"dotenv": "^16.0.2",
"ejs": "^3.1.8",
"express": "~4.18.1",
"express-prometheus-middleware": "^1.2.0",
"express-session": "^1.17.3",
"mongodb": "^4.9.1",
"morgan": "~1.10.0"
"morgan": "~1.10.0",
"redis": "^4.3.1"
},
"devDependencies": {
"eslint": "^8.23.1",
Expand Down
35 changes: 26 additions & 9 deletions src/server.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ import express from 'express'
import path from 'path'
import logger from 'morgan'
import session from 'express-session'
import { createClient as createRedisClient } from 'redis'
import connectRedis from 'connect-redis'
import { readFileSync } from 'fs'
const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)))

const app = new express()

Expand All @@ -35,14 +39,28 @@ app.set('view engine', 'ejs')
app.use(express.static(path.join(__dirname, 'public')))

// Session required for auth and MSAL signin flow
app.use(
session({
secret: '5Dyw14E3fEGHBWGPgw2X2dcEl8MVYhokBm1Ww5s2e0pe2wEryC8v3llGnGDm',
cookie: { secure: false },
resave: false,
saveUninitialized: false,
const sessionConfig = {
secret: packageJson.name,
cookie: { secure: false },
resave: false,
saveUninitialized: false,
}

// Very optional Redis session store - only really needed when running multiple instances
if (process.env.REDIS_SESSION_HOST) {
const RedisStore = connectRedis(session)
const redisClient = createRedisClient({ legacyMode: true, url: `redis://${process.env.REDIS_SESSION_HOST}` })
redisClient.connect().catch((err) => {
console.error('### 🚨 Redis session store error:', err.message)
process.exit(1)
})
)
sessionConfig.store = new RedisStore({ client: redisClient })
console.log('### 📚 Session store configured using Redis')
} else {
console.log('### 🎈 Session store not configured, sessions will not persist')
}

app.use(session(sessionConfig))

// Request logging, switch off when running tests
if (process.env.NODE_ENV !== 'test') {
Expand Down Expand Up @@ -81,8 +99,7 @@ if (process.env.TODO_MONGO_CONNSTR) {
}

// Make package app version a global var, shown in _foot.ejs
import { readFileSync } from 'fs'
const packageJson = JSON.parse(readFileSync(new URL('./package.json', import.meta.url)))

app.locals.version = packageJson.version

// Catch all route, generate an error & forward to error handler
Expand Down

0 comments on commit a633a37

Please sign in to comment.