import * as bodyParser from 'body-parser' ; import * as dotenv from 'dotenv' ; import * as express from 'express' ; import { filterXSS } from 'xss' ; import configs from './api/configs' ; import deliveryReports from './api/deliveryReports' ; import telnyx from './api/telnyx' ; dotenv . config ( ) ; import { connect } from './connection' ; import { debugBase , debugInit } from './debuggers' ; import { initBroker } from './messageBroker' ; import { trackEngages } from './trackers/engageTracker' ; export const app = express ( ) ; app . disable ( 'x-powered-by' ) ; trackEngages ( app ) ; app . get ( '/health' , async ( _req , res ) => { res . end ( 'ok' ) ; } ) ; app . use ( ( req , _res , next ) => { req . rawBody = '' ; req . on ( 'data' , chunk => { req . rawBody += chunk . toString ( ) . replace ( /\//g , '/' ) ; } ) ; next ( ) ; } ) ; app . use ( bodyParser . json ( ) ) ; app . use ( bodyParser . urlencoded ( { extended : true } ) ) ; app . use ( '/configs' , configs ) ; app . use ( '/deliveryReports' , deliveryReports ) ; app . use ( '/telnyx' , telnyx ) ; app . use ( ( error , _req , res , _next ) => { const msg = filterXSS ( error . message ) ; debugBase ( `Error: ` , msg ) ; res . status ( 500 ) . send ( msg ) ; } ) ; const { MONGO_URL , NODE_ENV , PORT , TEST_MONGO_URL } = process . env ; app . listen ( PORT , ( ) => { let mongoUrl = MONGO_URL ; if ( NODE_ENV === 'test' ) { mongoUrl = TEST_MONGO_URL ; } connect ( mongoUrl ) . then ( async ( ) => { initBroker ( app ) . catch ( e => { debugBase ( `Error ocurred during message broker init ${ e . message }` ) ; } ) ; } ) ; debugInit ( `Engages server is running on port ${ PORT }` ) ; } ) ;