-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
58 lines (47 loc) · 1.64 KB
/
app.js
File metadata and controls
58 lines (47 loc) · 1.64 KB
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
const express = require('express');
const winston = require('winston');
const compression = require('compression');
const cookieParser = require('cookie-parser');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const mongoose = require('./db/mongoose');
const app = express();
const logger = winston.createLogger({
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'combined.log' }),
]
});
app.use(bodyParser.json({
type: "*/*",
}));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(helmet());
app.use(mongoSanitize());
app.use(compression());
app.all('/*', function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,POST');
res.header('Access-Control-Allow-Headers', 'Content-type,Accept,X-Access-Token,X-Key,X-auth');
next();
});
// Define routes
// These MUST come after all app configurations or else they get fucked up.
// Reference: https://stackoverflow.com/questions/9177049/express-js-req-body-undefined
require('./components/event/eventRoutes.js')(app);
require('./components/user/userRoutes.js')(app);
// Error handler middleware
app.use((err, req, res, next) => {
logger.error(err);
if (!err.isOperational) {
// This is a programmer error and we need to do something special with it
// probably send an email and a slack notification.
}
res.status(err.status).json({
status: err.status,
message: err.message,
});
});
module.exports = app;