2016-04-30 23:41:09 +02:00
|
|
|
var express = require('express');
|
2016-05-01 15:43:25 +02:00
|
|
|
var favicon = require('serve-favicon');
|
2016-04-30 23:41:09 +02:00
|
|
|
var bodyParser = require('body-parser');
|
|
|
|
var cookieParser = require('cookie-parser');
|
2016-05-01 18:20:10 +02:00
|
|
|
var validator = require('express-validator');
|
2016-04-30 23:41:09 +02:00
|
|
|
var log4js = require('log4js');
|
|
|
|
var logger = require('./lib/logger');
|
|
|
|
var routes = require('./lib/routes');
|
2016-06-18 09:19:57 +02:00
|
|
|
var path = require('path');
|
2014-12-03 15:22:03 +01:00
|
|
|
|
2016-05-01 18:20:10 +02:00
|
|
|
module.exports = function(config) {
|
|
|
|
var app = express();
|
2016-06-18 09:19:57 +02:00
|
|
|
var router = express.Router();
|
2016-05-01 18:20:10 +02:00
|
|
|
var baseUriPath = config.baseUriPath || '';
|
2016-06-18 09:19:57 +02:00
|
|
|
var 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',
|
|
|
|
level: 'auto' // 3XX=WARN, 4xx/5xx=ERROR
|
|
|
|
}));
|
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;
|
|
|
|
};
|