-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
67 lines (59 loc) · 1.71 KB
/
app.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
67
import express from 'express';
import mongoose from 'mongoose';
import dotenv from 'dotenv';
import expressSession from 'express-session';
import MongoStore from 'connect-mongo';
import cors from 'cors';
import { gameRouter } from './routes/game.js';
import { rateRouter } from './routes/rate.js';
import { userRouter } from './routes/user.js';
import loginRequired from './utils/loginRequired.js';
export const app = express();
dotenv.config();
let appPort = 3000;
if (process.env.PORT) {
appPort = process.env.PORT;
}
// DB setup
mongoose.connect(process.env.DATABASE_PASSWORD, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to database.'));
// allow express get json in request body
app.use(express.json());
app.use(
cors({
origin: [
'http://localhost',
'http://localhost:3000',
'https://coderscamp2021.github.io',
'https://team-lp-project-4.herokuapp.com',
],
credentials: true,
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
}),
);
app.enable('trust proxy');
app.use(
expressSession({
name: 'team-lp-project-3',
resave: false,
saveUninitialized: false,
secret: process.env.SESSION_SECRET,
store: MongoStore.create({ mongoUrl: process.env.DATABASE_PASSWORD }),
cookie: {
secure: true,
maxAge: 1000 * 60 * 60 * 8, // 1000 * 60 * 60 * 24, one day (in miliseconds)
httpOnly: true,
sameSite: 'none',
},
proxy: true,
}),
);
app.use('/games', gameRouter);
app.use('/rate', rateRouter);
app.use('/user', userRouter);
app.listen(appPort, () =>
console.log(`Server running on http://localhost:${appPort}...`),
);
export default app;