@@ -4,6 +4,7 @@ import * as util from 'util';
44import chalk from 'chalk' ;
55
66import * as expressType from 'express' ;
7+ import * as proxyMiddlewareType from 'http-proxy-middleware' ;
78
89import { IonicEnvironment , LiveReloadFunction , LogLevel , ServeDetails , ServeOptions } from '../../definitions' ;
910import { isDevServerMessage } from '../../guards' ;
@@ -130,10 +131,20 @@ async function setupServer(env: IonicEnvironment, options: ServeMetaOptions): Pr
130131 */
131132async function createHttpServer ( env : IonicEnvironment , options : ServeMetaOptions ) : Promise < expressType . Application > {
132133 const { DEV_SERVER_PREFIX , injectDevServerScript, injectLiveReloadScript } = await import ( '../dev-server' ) ;
133- const [ WebSocket , express ] = await Promise . all ( [ import ( 'ws' ) , import ( 'express' ) ] ) ;
134134 const { LOGGER_STATUS_COLORS } = await import ( '../../lib/utils/logger' ) ;
135135
136+ const [
137+ WebSocket ,
138+ express ,
139+ proxyMiddleware ,
140+ ] = await Promise . all ( [
141+ import ( 'ws' ) ,
142+ import ( 'express' ) ,
143+ import ( 'http-proxy-middleware' ) ,
144+ ] ) ;
145+
136146 const app = express ( ) ;
147+ const project = await env . project . load ( ) ;
137148
138149 /**
139150 * http responder for /index.html base entrypoint
@@ -146,7 +157,7 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
146157 indexHtml = injectDevServerScript ( indexHtml ) ;
147158
148159 if ( options . livereload ) {
149- indexHtml = injectLiveReloadScript ( indexHtml , options . externalAddressRequired ? options . externalIP : 'localhost' , options . livereloadPort ) ;
160+ indexHtml = injectLiveReloadScript ( indexHtml , options . livereloadPort ) ;
150161 }
151162
152163 res . set ( 'Content-Type' , 'text/html' ) ;
@@ -197,6 +208,11 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
197208 } ) ;
198209 }
199210
211+ app . use ( ( req , res , next ) => {
212+ env . log . debug ( `${ req . method } ${ req . path } ` ) ;
213+ next ( ) ;
214+ } ) ;
215+
200216 app . get ( '/' , serveIndex ) ;
201217 app . use ( '/' , express . static ( options . wwwDir ) ) ;
202218
@@ -217,8 +233,27 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
217233 app . get ( '/cordova_plugins.js' , servePlatformResource ) ;
218234 app . get ( '/plugins/*' , servePlatformResource ) ;
219235
220- if ( options . proxy ) {
221- await setupProxies ( env , app ) ;
236+ const livereloadUrl = `http://localhost:${ options . livereloadPort } ` ;
237+ const pathPrefix = `/${ DEV_SERVER_PREFIX } /tiny-lr` ;
238+ const proxyMiddlewareCfg : proxyMiddlewareType . Config = { changeOrigin : true , ws : true , logLevel : 'warn' , logProvider : ( ) => env . log } ;
239+
240+ app . use ( pathPrefix , proxyMiddleware ( pathPrefix , { target : livereloadUrl , pathRewrite : { [ pathPrefix ] : '' } , ...proxyMiddlewareCfg } ) ) ;
241+
242+ if ( options . proxy && project . proxies ) {
243+ for ( const proxy of project . proxies ) {
244+ const opts = { target : proxy . proxyUrl , pathRewrite : { [ proxy . path ] : '' } , ...proxyMiddlewareCfg } ;
245+
246+ if ( proxy . proxyNoAgent ) {
247+ opts . agent = < any > false ; // TODO: type issue
248+ }
249+
250+ if ( proxy . rejectUnauthorized === false ) {
251+ opts . secure = false ;
252+ }
253+
254+ app . use ( proxy . path , proxyMiddleware ( proxy . path , opts ) ) ;
255+ env . log . info ( `Proxy created ${ chalk . bold ( proxy . path ) } => ${ chalk . bold ( opts . target ) } ` ) ;
256+ }
222257 }
223258
224259 app . get ( `/${ DEV_SERVER_PREFIX } /dev-server.js` , async ( req , res ) => {
@@ -288,24 +323,6 @@ async function createHttpServer(env: IonicEnvironment, options: ServeMetaOptions
288323 } ) ;
289324}
290325
291- async function setupProxies ( env : IonicEnvironment , app : expressType . Application ) {
292- const url = await import ( 'url' ) ;
293- const project = await env . project . load ( ) ;
294-
295- for ( const proxy of project . proxies || [ ] ) {
296- const opts : any = url . parse ( proxy . proxyUrl ) ;
297- if ( proxy . proxyNoAgent ) {
298- opts . agent = false ;
299- }
300-
301- opts . rejectUnauthorized = ! ( proxy . rejectUnauthorized === false ) ;
302-
303- const proxyMiddleware = await import ( 'proxy-middleware' ) ;
304- app . use ( proxy . path , < expressType . RequestHandler > proxyMiddleware ( opts ) ) ;
305- console . log ( 'Proxy added:' + proxy . path + ' => ' + url . format ( opts ) ) ;
306- }
307- }
308-
309326/**
310327 * http responder for cordova.js file
311328 */
0 commit comments