app.js
/**
 * Creates an express app and initialises it
 * @param {Object} options the options to initialise the app with
 * @return {bluebird} the promise resolving to the app object
 */
Creates an express app and initialises it
@param {Object} options the options to initialise the app with
@return {bluebird} the promise resolving to the app object
Creates an express app and initialises it @param { Object } options the options to initialise the app with @return { bluebird } the promise resolving to the app object
function initApp(options) {
    const app = express();
    app.logger = options.logger;    
    app.metrics = options.metrics;  
    app.conf = options.config;      
    app.info = packageInfo;         
    if (!app.conf.port) { app.conf.port = 8888; }
    if (!app.conf.interface) { app.conf.interface = '0.0.0.0'; }
    if (app.conf.compression_level === undefined) { app.conf.compression_level = 3; }
    if (app.conf.cors === undefined) { app.conf.cors = '*'; }
    if (app.conf.csp === undefined) {
        app.conf.csp = "default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'";
    }
    if (app.conf.proxy) {
        process.env.HTTP_PROXY = app.conf.proxy;
        if (app.conf.no_proxy_list) {
            if (Array.isArray(app.conf.no_proxy_list)) {
                process.env.NO_PROXY = app.conf.no_proxy_list.join(',');
            } else {
                process.env.NO_PROXY = app.conf.no_proxy_list;
            }
        }
    }
    if (!app.conf.log_header_whitelist) {
        app.conf.log_header_whitelist = [
            'cache-control', 'content-type', 'content-length', 'if-match',
            'user-agent', 'x-request-id'
        ];
    }
    app.conf.log_header_whitelist = new RegExp(`^(?:${app.conf.log_header_whitelist.map((item) => {
        return item.trim();
    }).join('|')})$`, 'i');
    if (!app.conf.spec) {
        app.conf.spec = `${__dirname}/spec.yaml`;
    }
    if (app.conf.spec.constructor !== Object) {
        try {
            app.conf.spec = yaml.safeLoad(fs.readFileSync(app.conf.spec));
        } catch (e) {
            app.logger.log('warn/spec', `Could not load the spec: ${e}`);
            app.conf.spec = {};
        }
    }
    if (!app.conf.spec.swagger) {
        app.conf.spec.swagger = '2.0';
    }
    if (!app.conf.spec.info) {
        app.conf.spec.info = {
            version: app.info.version,
            title: app.info.name,
            description: app.info.description
        };
    }
    app.conf.spec.info.version = app.info.version;
    if (!app.conf.spec.paths) {
        app.conf.spec.paths = {};
    }
    app.all('*', (req, res, next) => {
        if (app.conf.cors !== false) {
            res.header('access-control-allow-origin', app.conf.cors);
            res.header('access-control-allow-headers', 'accept, x-requested-with, content-type');
            res.header('access-control-expose-headers', 'etag');
        }
        if (app.conf.csp !== false) {
            res.header('x-xss-protection', '1; mode=block');
            res.header('x-content-type-options', 'nosniff');
            res.header('x-frame-options', 'SAMEORIGIN');
            res.header('content-security-policy', app.conf.csp);
            res.header('x-content-security-policy', app.conf.csp);
            res.header('x-webkit-csp', app.conf.csp);
        }
        sUtil.initAndLogRequest(req, app);
        next();
    });
    app.conf.user_agent = app.conf.user_agent || app.info.name;
    app.set('x-powered-by', false);
    app.set('etag', false);
    app.use(compression({ level: app.conf.compression_level }));
    app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({ extended: true }));
    return BBPromise.resolve(app);
}
function initApp ( options ) { const app = express ( ) ; app . logger = options . logger ; app . metrics = options . metrics ; app . conf = options . config ; app . info = packageInfo ; if ( ! app . conf . port ) { app . conf . port = 8888 ; } if ( ! app . conf . interface ) { app . conf . interface = '0.0.0.0' ; } if ( app . conf . compression_level === undefined ) { app . conf . compression_level = 3 ; } if ( app . conf . cors === undefined ) { app . conf . cors = '*' ; } if ( app . conf . csp === undefined ) { app . conf . csp = "default-src 'self'; object-src 'none'; media-src *; img-src *; style-src *; frame-ancestors 'self'" ; } if ( app . conf . proxy ) { process . env . HTTP_PROXY = app . conf . proxy ; if ( app . conf . no_proxy_list ) { if ( Array . isArray ( app . conf . no_proxy_list ) ) { process . env . NO_PROXY = app . conf . no_proxy_list . join ( ',' ) ; } else { process . env . NO_PROXY = app . conf . no_proxy_list ; } } } if ( ! app . conf . log_header_whitelist ) { app . conf . log_header_whitelist = [ 'cache-control' , 'content-type' , 'content-length' , 'if-match' , 'user-agent' , 'x-request-id' ] ; } app . conf . log_header_whitelist = new RegExp ( ` ${ app . conf . log_header_whitelist . map ( ( item ) => { return item . trim ( ) ; } ) . join ( '|' ) } ` , 'i' ) ; if ( ! app . conf . spec ) { app . conf . spec = ` ${ __dirname } ` ; } if ( app . conf . spec . constructor !== Object ) { try { app . conf . spec = yaml . safeLoad ( fs . readFileSync ( app . conf . spec ) ) ; } catch ( e ) { app . logger . log ( 'warn/spec' , ` ${ e } ` ) ; app . conf . spec = { } ; } } if ( ! app . conf . spec . swagger ) { app . conf . spec . swagger = '2.0' ; } if ( ! app . conf . spec . info ) { app . conf . spec . info = { version : app . info . version , title : app . info . name , description : app . info . description } ; } app . conf . spec . info . version = app . info . version ; if ( ! app . conf . spec . paths ) { app . conf . spec . paths = { } ; } app . all ( '*' , ( req , res , next ) => { if ( app . conf . cors !== false ) { res . header ( 'access-control-allow-origin' , app . conf . cors ) ; res . header ( 'access-control-allow-headers' , 'accept, x-requested-with, content-type' ) ; res . header ( 'access-control-expose-headers' , 'etag' ) ; } if ( app . conf . csp !== false ) { res . header ( 'x-xss-protection' , '1; mode=block' ) ; res . header ( 'x-content-type-options' , 'nosniff' ) ; res . header ( 'x-frame-options' , 'SAMEORIGIN' ) ; res . header ( 'content-security-policy' , app . conf . csp ) ; res . header ( 'x-content-security-policy' , app . conf . csp ) ; res . header ( 'x-webkit-csp' , app . conf . csp ) ; } sUtil . initAndLogRequest ( req , app ) ; next ( ) ; } ) ; app . conf . user_agent = app . conf . user_agent || app . info . name ; app . set ( 'x-powered-by' , false ) ; app . set ( 'etag' , false ) ; app . use ( compression ( { level : app . conf . compression_level } ) ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; return BBPromise . resolve ( app ) ; }
Creates an express app and initialises it
 @param {Object} options the options to initialise the app with
 @return {bluebird} the promise resolving to the app object
Creates an express app and initialises it @param { Object } options the options to initialise the app with @return { bluebird } the promise resolving to the app object
// the main application object // get the options and make them available in the app // the logging device // the metrics // this app's config options // this app's package info // ensure some sane defaults // eslint-disable-next-line max-len // set outgoing proxy // if there is a list of domains which should // not be proxied, set it // set up header whitelisting for logging // set up the spec // set the CORS and CSP headers // set up the user agent header string to use for requests // disable the X-Powered-By header // disable the ETag header - users should provide them! // enable compression // use the JSON body parser // use the application/x-www-form-urlencoded parser