-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (73 loc) · 2.63 KB
/
index.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* INCLUDES ****************************** */
const express = require('express');
const session = require('express-session');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const passport = require('passport');
const flash = require('flash');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const passportConfig = require('./Middleware/Passport/config');
/* **************************************** */
/* CONSTANTS ****************************** */
const RATELIMITHITS = 10;
const RATELIMITINTERVAL = 1 * 60 * 1000; // 1 Min
/* **************************************** */
/* INCLUDE CONFIGS ************************ */
dotenv.config();
const port = process.env.PORT || 3000
/* **************************************** */
/* INCLUDE ROUTES ************************ */
const example = require('./Components/Example');
const user = require('./Components/User');
/* **************************************** */
/* SETUP APP ****************************** */
const app = express();
app.use(bodyParser());
/* **************************************** */
/* SECURITY SETUP ************************* */
// Potentiall could run only on production
// Enable if you're behind a reverse proxy (Heroku, Bluemix, AWS ELB, Nginx, etc)
// see https://expressjs.com/en/guide/behind-proxies.html
// app.set('trust proxy', 1);
const limiter = rateLimit({
windowMs: RATELIMITINTERVAL,
max: RATELIMITHITS,
});
// only apply to requests that begin with /api/
app.use(limiter);
app.use(helmet());
/* **************************************** */
/* SESSION SETUP ************************** */
const sesh = {
secret: 'Oh hi there', // Replaced with proper setup in production
cookie: {
secure: false,
maxAge: 60000,
},
};
if (app.get('env') === 'production') {
app.set('trust proxy', 1); // trust first proxy
sesh.cookie.secure = true; // serve secure cookies
sesh.secret = process.env.SESSION_SECRET;
}
app.use(session(sesh));
/* **************************************** */
/* PASSPORT SETUP ************************* */
passportConfig.setup(passport);
app.use(passport.initialize());
app.use(passport.session());
/* **************************************** */
/* FLASH SETUP **************************** */
app.use(flash());
/* **************************************** */
/* DEFINE ROUTES ************************** */
app.use('/example', example);
app.use('/user', user);
/* **************************************** */
/* SERVER START *************************** */
app.listen(port, () => {
console.log(`App listening on port ${port}`);
});
/* **************************************** */
module.exports = app;