-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
66 lines (54 loc) · 1.99 KB
/
server.js
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
61
62
63
64
65
66
require('dotenv').config();
const cors = require('cors');
const express = require('express');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const RedisStore = require('connect-redis').default;
const { redisClient } = require('./clients/redisClient'); // Your Redis client
const app = express();
app.set('trust proxy', 1); // Trust the first proxy
app.use(express.json());
app.use(cookieParser());
// CORS configuration for the WIDE domain
const wideCorsOptions = {
origin: process.env.WIDE_DOMAIN,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
};
// CORS configuration for the WEB domain
const webCorsOptions = {
origin: process.env.WEB_DOMAIN,
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
allowedHeaders: ['Content-Type', 'Authorization'],
credentials: true,
};
let cookieConfig = {
httpOnly: true,
secure: process.env.COOKIE_USE_SECURE === 'true',
sameSite: process.env.COOKIE_SAME_SITE || 'lax',
maxAge: parseInt(process.env.COOKIE_EXPIRY_MILLISECONDS, 10) || 3600000
}
if (process.env.COOKIE_DOMAIN != 'LOCAL') {
cookieConfig.domain = process.env.COOKIE_DOMAIN;
}
console.log('cookieConfig', cookieConfig);
app.use(session({
name: 'chm.sid',
secret: process.env.SESSION_SECRET, // Secret used to sign the session ID cookie
store: new RedisStore({ client: redisClient, prefix: 'chm-session-' }),
resave: false,
saveUninitialized: false,
cookie: cookieConfig
}));
console.log('starting server for CORS origin: ', process.env.WEB_DOMAIN);
const wideDataService = require('./routes/wideDataService');
app.use('/wide', cors(wideCorsOptions), wideDataService);
const dataService = require('./routes/dataService');
app.use('/chm', cors(webCorsOptions), dataService);
// Define Port
const PORT = process.env.PORT || 3400;
// Start Server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});