-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
141 lines (101 loc) · 3.35 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const express = require('express');
const methodOverride = require('method-override');
const session = require('express-session');
const pgSession = require('connect-pg-simple')(session);
const db = require('./db');
const path = require('path');
const csrf = require('csurf');
require('dotenv').config();
/**
* ===================================
* Configurations and set up
* ===================================
*/
// Init express app
const app = express();
// Set up middleware
app.use(methodOverride('_method'));
app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({
extended: true
}));
app.use(express.static(path.join(__dirname, './public/')));
//Experimenting with helmet
const helmet = require('helmet');
app.use(helmet.hidePoweredBy());
//
const compression = require('compression');
app.use(compression());
// Set react-views to be the default view engine
const reactEngine = require('express-react-views').createEngine();
app.set('views', __dirname + '/views');
app.set('view engine', 'jsx');
app.engine('jsx', reactEngine);
// Set session and authentication routes. User remains on auth routes until validated/logged in.
app.use(session({
store: new pgSession({
pool: db.pool,
}),
secret: process.env.COOKIE_SECRET,
resave: false,
saveUninitialized: false,
name: 'sid',
cookie: {
maxAge: 86400000,
sameSite: true
}
}))
const csrfProtection = csrf();
app.use(csrfProtection);
const authRoutes = require('./routes/auth-routes');
const authController = require('./controllers/auth-controller');
const errorController = require('./controllers/404-controller');
const gearRoutes = require('./routes/gear-routes');
const listsRoutes = require('./routes/lists-routes');
const pedalboardsRoutes = require('./routes/pedalboards-routes');
const userProfileRoutes = require('./routes/user-profile-routes');
const userFeedRoutes = require('./routes/user-feed-routes');
app.use((req, res, next) => {
res.locals.csrfToken = req.csrfToken();
next();
})
app.use('/auth', authRoutes);
app.use('/', async (req, res, next) => {
req.session.invalidMsg = "";
if (req.session.userId) {
req.session.currentUser = await authController.getUserInfo(req.session.userId);
next();
} else {
//redirect to homepage with auth routes if user is not logged in
res.redirect('/auth');
}
})
app.get('/', async (req, res) => {
if (req.session.userId) {
req.session.currentUser = await authController.getUserInfo(req.session.userId);
console.log(req.session.currentUser);
res.render('home', { 'currentUser': req.session.currentUser });
}
})
app.use('/gear', gearRoutes);
app.use('/lists', listsRoutes);
app.use('/pedalboards', pedalboardsRoutes);
app.use('/user-profile', userProfileRoutes);
app.use('/user-feed', userFeedRoutes);
app.use(errorController.get404Page);
/**
* ===================================
* Listen to requests on port 3000
* ===================================
*/
const PORT = process.env.PORT || 3000;
const server = app.listen(PORT, () => console.log('~~~ Tuning in to the waves of port ' + PORT + ' ~~~'));
let onClose = function() {
server.close(async () => {
await db.poolEnd();
console.log('Process terminated')
})
};
process.on('SIGTERM', onClose);
process.on('SIGINT', onClose);