This repository was archived by the owner on Jul 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
60 lines (53 loc) · 1.58 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
// Imports
const express = require('express');
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
const passport = require('passport');
const connectionPool = require('./src/postgreSQL');
const path = require('path');
require('./routes/api_helper/passportConfig')(passport);
// Basic settings
const app = express();
const port = process.env.PORT || 3000;
// Static routes
app.use(express.static('static'));
// Middleware
app.use(
session({
key: 'userIdentifier',
secret: 'secret',
store: new pgSession({
pool: connectionPool,
tableName: 'user_session',
}),
resave: false,
saveUninitialized: false, // Save even if not registered
cookie: { maxAge: 30 * 86400000 },
})
);
app.use(express.json({ limit: '100kb' }));
app.use((req, res, next) => {
const allowedOrigins = ['http://localhost:8080'];
const origin = req.headers.origin;
if (allowedOrigins.includes(origin)) {
res.setHeader('Access-Control-Allow-Origin', origin);
}
res.header('Access-Control-Allow-Credentials', 'true');
res.header(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, content-type, Accept'
);
req.db = connectionPool; // pass the database connection pool to each request
next();
});
app.use(passport.initialize());
app.use(passport.session());
// Routes
app.use('/api', require('./routes/api'));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'static', 'index.html'));
});
// listen to something
app.listen(port, () =>
console.log(`Example app listening at http://localhost:${port}`)
);