mirror of
https://github.com/Unleash/unleash.git
synced 2024-12-28 00:06:53 +01:00
9fdb948c69
Sessions will be required to solve admin-auth. I also refactored a few middlewares into seperate files to make the code easier to read. closes #262
58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const express = require('express');
|
|
const favicon = require('serve-favicon');
|
|
const bodyParser = require('body-parser');
|
|
const cookieParser = require('cookie-parser');
|
|
const routes = require('./routes');
|
|
const path = require('path');
|
|
const errorHandler = require('errorhandler');
|
|
const unleashSession = require('./middleware/session');
|
|
const responseTime = require('./middleware/response-time');
|
|
const requestLogger = require('./middleware/request-logger');
|
|
const validator = require('./middleware/validator');
|
|
|
|
module.exports = function(config) {
|
|
const app = express();
|
|
|
|
const baseUriPath = config.baseUriPath || '';
|
|
|
|
app.set('trust proxy');
|
|
app.disable('x-powered-by');
|
|
app.set('port', config.port);
|
|
app.locals.baseUriPath = baseUriPath;
|
|
|
|
if (typeof config.preHook === 'function') {
|
|
config.preHook(app);
|
|
}
|
|
|
|
app.use(cookieParser());
|
|
app.use(bodyParser.json({ strict: false }));
|
|
app.use(unleashSession(config));
|
|
app.use(responseTime(config));
|
|
app.use(requestLogger(config));
|
|
app.use(validator(config));
|
|
|
|
if (config.publicFolder) {
|
|
app.use(favicon(path.join(config.publicFolder, 'favicon.ico')));
|
|
app.use(baseUriPath, express.static(config.publicFolder));
|
|
}
|
|
|
|
if (typeof config.preRouterHook === 'function') {
|
|
config.preRouterHook(app);
|
|
}
|
|
|
|
// Setup API routes
|
|
const middleware = routes.router(config);
|
|
if (!middleware) {
|
|
throw new Error('Routes invalid');
|
|
}
|
|
app.use(`${baseUriPath}/`, middleware);
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
app.use(errorHandler());
|
|
}
|
|
|
|
return app;
|
|
};
|