import { ApplicationConfig } from '@loopback/core' ; import { once } from 'events' ; import express , { Request , Response } from 'express' ; import http from 'http' ; import { AddressInfo } from 'net' ; import path from 'path' ; import { CoffeeShopApplication } from './application' ; const loopback = require ( 'loopback' ) ; const compression = require ( 'compression' ) ; const cors = require ( 'cors' ) ; const helmet = require ( 'helmet' ) ; export { ApplicationConfig } ; export class ExpressServer { private app : express . Application ; public readonly lbApp : CoffeeShopApplication ; public server ? : http . Server ; public url : String ; constructor ( options = { } ) { this . app = express ( ) ; this . lbApp = new CoffeeShopApplication ( options ) ; this . app . use ( loopback . favicon ( ) ) ; this . app . use ( compression ( ) ) ; this . app . use ( cors ( ) ) ; this . app . use ( helmet ( ) ) ; this . app . use ( '/api' , this . lbApp . requestHandler ) ; this . app . get ( '/ping' , function ( _req , res ) { res . send ( 'pong' ) ; } ) ; this . app . use ( express . static ( path . join ( __dirname , '../public' ) ) ) ; } public async boot ( ) { await this . lbApp . boot ( ) ; } public async start ( ) { await this . lbApp . start ( ) ; const port = this . lbApp . restServer . config . port ?? 3000 ; const host = this . lbApp . restServer . config . host ?? '127.0.0.1' ; this . server = this . app . listen ( port , host ) ; await once ( this . server , 'listening' ) ; const add = < AddressInfo > this . server . address ( ) ; this . url = `http://${ add . address }:${ add . port }` ; } public async stop ( ) { if ( ! this . server ) return ; await this . lbApp . stop ( ) ; this . server . close ( ) ; await once ( this . server , 'close' ) ; this . server = undefined ; } }