1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-28 00:06:53 +01:00
unleash.unleash/packages/unleash-api/app.js
Ivar d8f66b7136 Introduces the /api path.
Some endpoints are provided on both / and /api
to support older clients.

Closes #162
2020-02-20 08:30:37 +01:00

55 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 validator = require('express-validator');
const log4js = require('log4js');
const logger = require('./lib/logger');
const routes = require('./lib/routes');
const path = require('path');
module.exports = function (config) {
const app = express();
const baseUriPath = config.baseUriPath || '';
const publicFolder = config.publicFolder;
app.set('trust proxy');
app.set('port', config.port);
app.locals.baseUriPath = baseUriPath;
app.use(cookieParser());
if (publicFolder) {
app.use(favicon(path.join(publicFolder, 'favicon.ico')));
}
app.use(validator([]));
if (publicFolder) {
app.use(baseUriPath, express.static(publicFolder));
}
app.use(bodyParser.json({ strict: false }));
app.use(log4js.connectLogger(logger, {
format: ':remote-addr :status :method :url :response-timems',
level: 'auto', // 3XX=WARN, 4xx/5xx=ERROR
}));
// Setup API routes
const apiRouter = express.Router(); // eslint-disable-line new-cap
routes.createAPI(apiRouter, config);
app.use(`${baseUriPath}/api/`, apiRouter);
// Setup deprecated routes
const router = express.Router(); // eslint-disable-line new-cap
routes.createLegacy(router, config);
app.use(baseUriPath, router);
if (process.env.NODE_ENV !== 'production') {
app.use(require('errorhandler')());
}
return app;
};