@@ -51,6 +51,18 @@ var RoutingProxy = exports.RoutingProxy = function (options) {
51
51
this . https = this . source . https || options . https ;
52
52
this . enable = options . enable ;
53
53
this . forward = options . forward ;
54
+
55
+ //
56
+ // Listen for 'newListener' events so that we can bind 'proxyError'
57
+ // listeners to each HttpProxy's 'proxyError' event.
58
+ //
59
+ this . on ( 'newListener' , function ( evt ) {
60
+ if ( evt === 'proxyError' || evt === 'webSocketProxyError' ) {
61
+ Object . keys ( self . proxies ) . forEach ( function ( key ) {
62
+ self . proxies [ key ] . on ( evt , this . emit . bind ( this , evt ) ) ;
63
+ } ) ;
64
+ }
65
+ } ) ;
54
66
} ;
55
67
56
68
@@ -90,8 +102,12 @@ RoutingProxy.prototype.add = function (options) {
90
102
} ) ;
91
103
92
104
this . proxies [ key ] = new HttpProxy ( options ) ;
93
- this . proxies [ key ] . on ( 'proxyError' , this . emit . bind ( this , 'proxyError' ) ) ;
94
- this . proxies [ key ] . on ( 'webSocketProxyError' , this . emit . bind ( this , 'webSocketProxyError' ) ) ;
105
+ if ( this . listeners ( 'proxyError' ) . length > 0 ) {
106
+ this . proxies [ key ] . on ( 'proxyError' , this . emit . bind ( this , 'proxyError' ) ) ;
107
+ }
108
+ if ( this . listeners ( 'webSocketProxyError' ) . length > 0 ) {
109
+ this . proxies [ key ] . on ( 'webSocketProxyError' , this . emit . bind ( this , 'webSocketProxyError' ) ) ;
110
+ }
95
111
this . proxies [ key ] . on ( 'start' , this . emit . bind ( this , 'start' ) ) ;
96
112
this . proxies [ key ] . on ( 'forward' , this . emit . bind ( this , 'forward' ) ) ;
97
113
this . proxies [ key ] . on ( 'end' , this . emit . bind ( this , 'end' ) ) ;
@@ -188,7 +204,6 @@ RoutingProxy.prototype.proxyRequest = function (req, res, options) {
188
204
189
205
if ( ! this . proxies [ key ] ) {
190
206
this . add ( options ) ;
191
-
192
207
}
193
208
194
209
proxy = this . proxies [ key ] ;
0 commit comments