import { Express , Router , RequestHandler , ErrorRequestHandler } from 'express' ; import { AppConfig } from './config' ; import express = require ( 'express' ) ; import bodyParser = require ( 'body-parser' ) ; import morgan = require ( 'morgan' ) ; import { Logger , LoggerFactory } from './common' ; export class ExpressAppFactory { private static readonly LOGGER : Logger = LoggerFactory . getLogger ( ) ; private constructor ( ) { } static getExpressApp ( appConfig , apiRouter , preApiRouterMiddlewareFns , postApiRouterMiddlewareFns ) { const app = express ( ) ; app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; app . use ( bodyParser . json ( ) ) ; if ( appConfig . serveStatic ) { ExpressAppFactory . LOGGER . info ( `Serving static files from public` ) ; app . use ( express . static ( 'public' ) ) ; } if ( appConfig . enableHttpRequestLogging ) { ExpressAppFactory . LOGGER . info ( `Request logging is enabled` ) ; app . use ( morgan ( 'combined' ) ) ; } if ( preApiRouterMiddlewareFns != null ) { postApiRouterMiddlewareFns . forEach ( ( middlewareFn ) => app . use ( middlewareFn ) ) ; } app . use ( '/api' , apiRouter ) ; if ( postApiRouterMiddlewareFns != null ) { postApiRouterMiddlewareFns . forEach ( ( middlewareFn ) => app . use ( middlewareFn ) ) ; } return app ; } }