-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
46 lines (42 loc) · 1.19 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
require('./api/config/DBConnection');
const express = require('express'),
logger = require('morgan'),
cors = require('cors'),
helmet = require('helmet'),
compression = require('compression'),
bodyParser = require('body-parser'),
routes = require('./api/routes');
const errorLog = require('./api/utils/ErrorLogger');
app = express();
app.use(logger(process.env.NODE_ENV === 'production' ? 'combined' : 'dev'));
app.use(
cors({
origin: true,
credentials: true,
methods: ['GET', 'POST', 'PATCH', 'DELETE']
})
);
app.use(helmet());
app.use(compression());
app.use(bodyParser.json());
app.use('/api', routes);
app.use(errorLog);
// 500 internal server error handler
app.use((err, req, res, next) => {
if (err.statusCode === 404) return next();
res.status(500).json({
// Never leak the stack trace of the err if running in production mode
err: process.env.NODE_ENV === 'production' ? null : err,
msg: '500 Internal Server Error',
data: null
});
});
// 404 error handler
app.use((req, res) => {
res.status(404).json({
err: null,
msg: '404 Not Found',
data: null
});
});
module.exports = app;