1+ const CORS = require ( 'cors' )
2+ const Pify = require ( 'pify' )
3+ const { URL } = require ( 'url' )
14const net = require ( 'net' )
25const http = require ( 'http' )
36const https = require ( 'https' )
4-
5- const Pify = require ( 'pify' )
6- const debug = require ( 'debug' ) ( 'roshub.server' )
7- const Hoek = require ( 'hoek' )
87const morgan = require ( 'morgan' )
98const express = require ( 'express' )
10- const CORS = require ( 'cors' )
11- const expressListRoutes = require ( 'express-list-routes' )
129const bodyParser = require ( 'body-parser' )
13- const { URL } = require ( 'url' ) ;
10+ const expressListRoutes = require ( 'express-list-routes' )
11+ const debug = require ( 'debug' ) ( 'dataparty.service-host' )
12+
1413
1514
1615class ServiceHost {
1716 constructor ( {
18- listenUri = 'http://0.0.0.0:4001' ,
19- cors = null ,
20- trust_proxy = false
21- } ) {
17+ cors = { } ,
18+ trust_proxy = false ,
19+ listenUri = 'http://0.0.0.0:4001'
20+ } = { } ) {
2221 this . apiApp = express ( )
2322 this . router = express . Router ( )
2423
25-
2624 if ( cors ) {
2725 this . apiApp . use ( CORS ( ) )
2826 this . apiApp . options ( '*' , CORS ( cors ) )
@@ -42,6 +40,10 @@ class ServiceHost {
4240 this . apiServerUri = new URL ( listenUri )
4341 }
4442
43+ async onRequest ( req , res ) {
44+ debug ( 'request' )
45+ res . end ( 'nope' )
46+ }
4547
4648 async start ( ) {
4749
@@ -50,13 +52,13 @@ class ServiceHost {
5052 if ( this . apiServer == null ) {
5153 debug ( 'adding default endpoints' )
5254 //Setup router
53- // this.apiApp.use(this.router);
55+ this . apiApp . use ( this . onRequest . bind ( this ) )
5456
5557 if ( debug . enabled ) { expressListRoutes ( 'API:' , this . router ) }
5658 }
5759
5860 let listenPort = this . apiServerUri . port
59- let listenHost = this . apiServerUri . host
61+ let listenHost = this . apiServerUri . hostname
6062
6163 if ( this . apiServerUri . protocol == 'http:' ) {
6264
@@ -77,7 +79,11 @@ class ServiceHost {
7779
7880 }
7981
80- await ( Pify ( this . apiServer . listen ) ( listenPort , listenHost ) )
82+ debug ( 'server listening' , this . apiServerUri . toString ( ) )
83+
84+ await new Promise ( ( resolve , reject ) => {
85+ this . apiServer . listen ( listenPort , listenHost , resolve )
86+ } )
8187
8288 clearTimeout ( this . errorHandlerTimer )
8389 this . errorHandlerTimer = null
0 commit comments