2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2016-06-18 21:53:18 +02:00
|
|
|
const express = require('express');
|
|
|
|
const favicon = require('serve-favicon');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const validator = require('express-validator');
|
2016-12-01 17:43:08 +01:00
|
|
|
const responseTime = require('response-time');
|
2016-06-18 21:53:18 +02:00
|
|
|
const log4js = require('log4js');
|
2016-11-13 21:07:14 +01:00
|
|
|
const logger = require('./logger');
|
|
|
|
const routes = require('./routes');
|
2016-06-18 21:53:18 +02:00
|
|
|
const path = require('path');
|
2016-11-13 15:31:28 +01:00
|
|
|
const errorHandler = require('errorhandler');
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-12-04 14:09:37 +01:00
|
|
|
const { REQUEST_TIME } = require('./events');
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
module.exports = function(config) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = express();
|
2016-11-09 22:31:49 +01:00
|
|
|
|
2016-12-27 21:03:50 +01:00
|
|
|
const baseUriPath = config.baseUriPath || '';
|
2016-06-18 21:53:18 +02:00
|
|
|
const publicFolder = config.publicFolder;
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
app.set('trust proxy');
|
2016-05-01 22:59:43 +02:00
|
|
|
app.set('port', config.port);
|
2016-05-01 18:20:10 +02:00
|
|
|
app.locals.baseUriPath = baseUriPath;
|
2016-12-28 21:04:26 +01:00
|
|
|
|
|
|
|
if (typeof config.preHook === 'function') {
|
|
|
|
config.preHook(app);
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(cookieParser());
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-11-12 11:24:50 +01:00
|
|
|
if (publicFolder) {
|
2016-06-18 09:19:57 +02:00
|
|
|
app.use(favicon(path.join(publicFolder, 'favicon.ico')));
|
|
|
|
}
|
2016-11-12 11:24:50 +01:00
|
|
|
|
2017-06-28 10:17:14 +02:00
|
|
|
app.use(
|
|
|
|
responseTime((req, res, time) => {
|
|
|
|
const timingInfo = {
|
|
|
|
path: req.path,
|
|
|
|
method: req.method,
|
|
|
|
statusCode: res.statusCode,
|
|
|
|
time,
|
|
|
|
};
|
|
|
|
config.eventBus.emit(REQUEST_TIME, timingInfo);
|
|
|
|
})
|
|
|
|
);
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(validator([]));
|
2016-12-01 17:43:08 +01:00
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (publicFolder) {
|
|
|
|
app.use(baseUriPath, express.static(publicFolder));
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(bodyParser.json({ strict: false }));
|
2016-12-03 14:09:09 +01:00
|
|
|
|
2016-12-04 14:09:37 +01:00
|
|
|
if (config.enableRequestLogger) {
|
2017-06-28 10:17:14 +02:00
|
|
|
app.use(
|
|
|
|
log4js.connectLogger(logger, {
|
|
|
|
format: ':status :method :url :response-timems',
|
|
|
|
level: 'auto', // 3XX=WARN, 4xx/5xx=ERROR
|
|
|
|
})
|
|
|
|
);
|
2016-12-04 14:09:37 +01:00
|
|
|
}
|
2015-03-10 16:30:56 +01:00
|
|
|
|
2016-12-28 21:04:26 +01:00
|
|
|
if (typeof config.preRouterHook === 'function') {
|
|
|
|
config.preRouterHook(app);
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
// Setup API routes
|
2017-06-28 10:20:22 +02:00
|
|
|
const middleware = routes.router(config);
|
|
|
|
if (!middleware) {
|
|
|
|
throw new Error('Routes invalid');
|
|
|
|
}
|
|
|
|
app.use(`${baseUriPath}/`, middleware);
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
2016-11-13 15:31:28 +01:00
|
|
|
app.use(errorHandler());
|
2016-06-18 09:19:57 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
return app;
|
|
|
|
};
|