-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
59 lines (47 loc) · 1.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
const bodyParser = require('body-parser');
const compress = require('compression');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const express = require('express');
const expressWinston = require('express-winston');
const helmet = require('helmet');
const morgan = require('morgan');
const errors = require('./core/common/errors');
const handle = require('./middlewares/handle-errors');
const hooks = require('./hooks/index');
const logger = require('./core/common/loggers').get('HTTP');
const routes = require('./routes/index');
const app = express();
// If the log level is debug, log all HTTP requests and responses.
if (process.env.LOG_LEVEL === 'debug') {
app.use(morgan('dev'));
expressWinston.requestWhitelist.push('body');
expressWinston.responseWhitelist.push('body');
app.use(expressWinston.logger({
winstonInstance: logger,
meta: true,
msg: 'HTTP {{ req.method }} {{ req.url }} {{ res.statusCode }} {{ res.responseTime }}ms',
colorStatus: true
}));
}
// Parse body parameters and attach them to req.body.
const limit = `${ process.env.MAX_REQUEST_BODY_SIZE }kb`;
app.use(bodyParser.json({ limit }));
app.use(bodyParser.urlencoded({ extended: true, limit }));
app.use(cookieParser());
app.use(compress());
// Secure the application by setting various HTTP headers.
app.use(helmet());
// Enable CORS (Cross Origin Resource Sharing).
app.use(cors());
// Mount application routes on /api path.
app.use('/api', routes);
// Mount hooks on /hooks path.
app.use('/hooks', hooks);
// Catch requests to unknown endpoints, and forward them to the error handler.
app.use((req, _res, _next) => {
throw new errors.NotFoundError(`Nothing to ${ req.method } @ ${ req.url }.`);
});
// Set the error handler.
app.use(handle);
module.exports = app;