-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
94 lines (74 loc) · 2.88 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
const debug = require('debug')('app');
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const hbs = require('hbs');
const express_enforces_ssl = require('express-enforces-ssl');
const hsts = require('hsts');
const compress = require('compression');
const cookieSession = require('cookie-session');
const serverStarted = require(`./bin/middleware/server-started`);
const checkSession = require(`./bin/middleware/check-session`);
const protectRoute = require(`./bin/middleware/protect-route`);
const messages = require(`./bin/middleware/messages`);
const app = express();
app.set('etag', false);
// Enforce HTTPS
app.enable('trust proxy');
if(process.env.NODE_ENV === "production"){
app.use(express_enforces_ssl());
app.use(hsts({
maxAge: 86400 // 1 day in seconds
}));
}
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
hbs.registerPartials(__dirname + '/views/partials');
require(`${__dirname}/bin/lib/helpers`)(hbs);
app.use(compress());
app.use( logger('dev') );
app.use(express.json( { limit: '150mb'} ) );
app.use(express.urlencoded( { extended: false } ) );
app.use( cookieParser() );
app.use( messages );
const staticCaching = process.env.NODE_ENV === "production" ? { maxAge: (60 * 60 * 72 * 1000).toString()} : {maxAge : 0} // 3 days in prod || 0 in development
app.use( express.static( path.join(__dirname, 'public' ), staticCaching ));
app.use(cookieSession({
name: 'choirless-session',
secret : process.env.SESSION_SECRET,
maxAge: 72 * 60 * 60 * 1000, // 3 days
secure : process.env.NODE_ENV === "production"
}));
app.use('*', serverStarted);
app.use('*', checkSession);
app.use('/', require(`${__dirname}/routes/index`));
app.use('/account', require(`${__dirname}/routes/account`));
app.use('/dashboard', [protectRoute], require(`${__dirname}/routes/dashboard`));
app.use('/choir', [protectRoute], require(`${__dirname}/routes/choir`));
app.use('/performance', [protectRoute], require(`${__dirname}/routes/performance`));
app.use('/admin', [protectRoute], require(`${__dirname}/routes/admin`));
app.use('/feedback', [protectRoute], require(`${__dirname}/routes/feedback`));
app.get('/magic-camera-selector', (req, res, next) => {
res.render('magic-camera-selector');
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404, `We can't find that resource`));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
const statusCode = err.status || 500;
// render the error page
res.status(statusCode);
res.render('error', {
bodyid : 'error',
status : statusCode
});
});
module.exports = app;