import bodyParser = require ( 'body-parser' ) ; import { HttpServer } from '../../../types' ; /**
 * HTTP server that uses Express under the hood.
 */ export class ExpressHttpServer implements HttpServer { protected app ; private listening = false ; private server ; param port param hostname constructor ( private port , private hostname , middleware ? ) { const express = require ( 'express' ) ; this . app = express ( ) ; this . app . use ( bodyParser . json ( ) ) ; if ( typeof middleware !== 'undefined' ) { for ( const m of middleware ) { this . app . use ( m ( ) ) ; } } this . initRoutes ( ) ; } /**
     * Initializes any app routes.
     * App has no routes by default.
     */ protected initRoutes ( ) { return ; } /**
     * Starts the server.
     */ public async listen ( ) { if ( this . listening ) { return ; } return new Promise < void > ( ( resolve , reject ) => { this . server = this . app . listen ( this . port , this . hostname , ( ) => { this . listening = true ; resolve ( ) ; } ) ; } ) ; } /**
     * Stops the server.
     */ public async close ( ) { if ( ! this . listening ) { return ; } await this . server . close ( ) ; this . listening = false ; } }