-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-with-redis.js
More file actions
60 lines (47 loc) · 1.42 KB
/
server-with-redis.js
File metadata and controls
60 lines (47 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import express from 'express';
import bodyParser from 'body-parser';
import routes from './routes/index.js';
import session from 'express-session';
import passport from 'passport';
import configurePassport from './middleware/configure-passport.js';
import { sessionConfig, redisConfig } from './config.js';
import { createClient } from 'redis';
import RedisStore from 'connect-redis';
const app = express();
const port = 3200;
// Create a Redis client
const redisClient = createClient({
socket: {
host: redisConfig.host,
port: redisConfig.port
},
password: redisConfig.password
});
await redisClient.connect();
// Create a RedisStore instance
const store = new RedisStore({
client: redisClient,
prefix: 'vlab:sess:' // Optional, add a prefix for better distinction
});
// Configure sessionConfig, attach the store
sessionConfig.store = store;
// Use the session middleware
app.use(session(sessionConfig));
// Other settings remain the same
app.set('trust proxy', 1);
app.set('view engine', 'ejs');
app.set('views', './views');
app.use(express.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("public"));
app.use(passport.initialize());
app.use(passport.session());
configurePassport(passport);
app.use((req, res, next) => {
res.locals.authUser = req.user;
next();
});
app.use('/', routes);
app.listen(port, () => {
console.log(`🚀 Server is running on http://localhost:${port}`);
});