-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
148 lines (130 loc) · 3.77 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
142
143
144
145
146
147
148
// Module dependencies
const express = require('express');
const cookieParser = require('cookie-parser');
const compress = require('compression');
const favicon = require('serve-favicon');
const session = require('express-session');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const errorHandler = require('errorhandler');
const methodOverride = require('method-override');
const MongoStore = require('connect-mongo');
const path = require('path');
const passport = require('passport');
const expressValidator = require('express-validator');
const assets = require('express-asset-versions');
const Agendash = require('agendash');
const cors = require('cors');
const cloudinary = require('cloudinary').v2;
// Logging configuration
const logger = require('./nodejs/log');
// API keys and configuration.
const secrets = require('./config/secrets');
const response = require('./nodejs/response');
// Cloudinary global config
cloudinary.config(secrets.cloudinary);
// Development options
let cookieOpts = {
httpOnly: false,
secure: false,
}; // Unsecure cookies
let publicOpts = {
maxAge: 0,
}; // No cached content
const app = express();
require('./nodejs/db');
// Express configuration
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(compress());
app.use(
morgan('dev', {
skip(req, res) {
return res.statusCode < 400;
}, // log only HTTP request errors
})
);
app.use(favicon(path.join(__dirname, 'frontend/public', 'favicon.png')));
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(expressValidator());
app.use(methodOverride());
app.use(cookieParser());
// Disable CORS
app.use(cors());
app.options('*', cors()); // Enable CORS Pre-Flight (Since I have custom headers for the token)
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept, x-xsrf-token');
next();
});
function requireHTTPS(req, res, next) {
if (!req.secure) {
return res.redirect(`https://${req.get('host')}${req.url}`);
}
next();
}
if (app.get('env') === 'production') {
publicOpts = {
maxAge: 86400000,
}; // Max age of 1 day for static content
app.set('trust proxy', 1); // trust first proxy
app.use(requireHTTPS); // HTTPS redirection
cookieOpts = {
httpOnly: true,
secure: true,
}; // Secure cookies
}
app.use(
session({
resave: true,
saveUninitialized: true,
secret: secrets.sessionSecret,
store: MongoStore.create({
mongoUrl: `${secrets.MongoUrl + secrets.db}?authSource=admin`,
mongoOptions: {},
}),
cookie: cookieOpts,
})
);
app.use(express.static(path.join(__dirname, 'public'), publicOpts));
app.use('/', express.static(path.join(__dirname, 'frontend/build'), publicOpts));
app.use(assets('', path.join(__dirname, 'public'))); // Append checksum to files
app.use(passport.initialize());
app.use(passport.session());
// Agenda scheduler
const { agenda } = require('./nodejs/agenda');
// Agenda UI Dashboard
app.use(
'/agenda',
(req, res, next) => {
if (!req.user) {
res.send(401);
} else {
next();
}
},
Agendash(agenda)
);
// App routes
require('./routes')(app);
if (app.get('env') === 'production') {
// production error handler, no stacktraces shown
// eslint-disable-next-line no-unused-vars
app.use((err, req, res, next) => {
logger.error(`500 error: ${err}`);
res.status(err.status || 500);
res.send({ error: 'Internal Server Error' });
});
} else {
app.use(errorHandler()); // Display stack trace in dev
}
app.post('*', (req, res) => {
response.notFound(res, `Path "${req.path}" not found`);
});
module.exports = app;