-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (87 loc) · 2.46 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
// Load environment variables for development
require('dotenv').config();
const express = require('express');
const path = require('path');
const ejs = require('ejs');
const session = require('express-session');
const passport = require('passport');
const flash = require('connect-flash');
const methodOverride = require('method-override');
const MemoryStore = require('memorystore')(session);
const db = require('./config/database');
const helmet = require('helmet');
const compression = require('compression');
const app = express();
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
// Helmet
app.use(helmet());
// Compression
app.use(compression());
// Body Parser
app.use(express.json());
app.use(express.urlencoded({extended: false}));
// Method override
app.use(methodOverride('_method'));
// Session
let sess = {
store: new MemoryStore({
checkPeriod: 1000 * 60 * 60 * 24,
}),
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
rolling: true,
cookie: {
maxAge: 1000 * 60 * 60 * 24,
httpOnly: true,
}
};
if (process.env.NODE_ENV === 'production') {
app.set('trust proxy', 1);
sess.cookie.secure = true;
sess.cookie.domain = process.env.DOMAIN;
}
app.use(session(sess));
// Connect flash and express messages
app.use(flash());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Passport
require('./config/auth-google')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.use((req, res, next) => {
res.locals.user = req.user;
next()
});
// Middleware for poll voting functionality
app.use((req, res, next) => {
req.session.pollsVotedFor = req.session.pollsVotedFor || [];
next();
});
// Routes
const indexRoutes = require('./routes/index');
const pollRoutes = require('./routes/polls');
const authRoutes = require('./routes/auth');
const userRoutes = require('./routes/user');
app.use('/', indexRoutes);
app.use('/polls', pollRoutes);
app.use('/', authRoutes);
app.use('/my-polls', userRoutes);
// Catch all route
app.get('*', (req, res, next) => {
let err = new Error('Page not found');
err.status = 404;
return next(err);
});
// Error handler
app.use((err, req, res, next) => {
res.render('index/error', {error: err});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log('Server started on port ' + PORT);
});