2016-06-18 21:53:18 +02:00
|
|
|
'use strict';
|
|
|
|
const express = require('express');
|
|
|
|
const favicon = require('serve-favicon');
|
|
|
|
const bodyParser = require('body-parser');
|
|
|
|
const cookieParser = require('cookie-parser');
|
|
|
|
const validator = require('express-validator');
|
|
|
|
const log4js = require('log4js');
|
|
|
|
const logger = require('./lib/logger');
|
|
|
|
const routes = require('./lib/routes');
|
|
|
|
const path = require('path');
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-07-02 11:54:50 +02:00
|
|
|
module.exports = function (config) {
|
2016-06-18 21:53:18 +02:00
|
|
|
const app = express();
|
2016-06-18 22:23:19 +02:00
|
|
|
const router = express.Router(); // eslint-disable-line new-cap
|
2016-06-18 21:53:18 +02:00
|
|
|
const baseUriPath = config.baseUriPath || '';
|
|
|
|
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-05-01 22:59:43 +02:00
|
|
|
app.use(cookieParser());
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (publicFolder) {
|
|
|
|
app.use(favicon(path.join(publicFolder, 'favicon.ico')));
|
|
|
|
}
|
|
|
|
|
2016-05-01 22:59:43 +02:00
|
|
|
app.use(validator([]));
|
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-05-01 18:20:10 +02:00
|
|
|
app.use(log4js.connectLogger(logger, {
|
|
|
|
format: ':remote-addr :status :method :url :response-timems',
|
2016-06-18 21:55:46 +02:00
|
|
|
level: 'auto', // 3XX=WARN, 4xx/5xx=ERROR
|
2016-05-01 18:20:10 +02:00
|
|
|
}));
|
2015-03-10 16:30:56 +01:00
|
|
|
|
2016-05-01 22:53:09 +02:00
|
|
|
// Setup API routes
|
|
|
|
routes.create(router, config);
|
2016-05-01 18:20:10 +02:00
|
|
|
app.use(baseUriPath, router);
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-06-18 09:19:57 +02:00
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
app.use(require('errorhandler')());
|
|
|
|
}
|
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
return app;
|
|
|
|
};
|