@@ -7,8 +7,7 @@ var sockjs = require("sockjs");
7
7
var StreamCache = require ( "stream-cache" ) ;
8
8
var http = require ( "http" ) ;
9
9
var https = require ( "https" ) ;
10
- var httpProxy = require ( "http-proxy" ) ;
11
- var proxy = new httpProxy . createProxyServer ( { secure : false } ) ;
10
+ var httpProxyMiddleware = require ( "http-proxy-middleware" ) ;
12
11
var serveIndex = require ( "serve-index" ) ;
13
12
var historyApiFallback = require ( "connect-history-api-fallback" ) ;
14
13
@@ -120,42 +119,69 @@ function Server(compiler, options) {
120
119
121
120
proxy : function ( ) {
122
121
if ( options . proxy ) {
123
- if ( ! Array . isArray ( options . proxy ) ) {
124
- options . proxy = Object . keys ( options . proxy ) . map ( function ( path ) {
122
+ /**
123
+ * Assume a proxy configuration specified as:
124
+ * proxy: 'a url'
125
+ */
126
+ if ( typeof options . proxy === 'string' ) {
127
+ options . proxy = [ {
128
+ context : options . proxy
129
+ } ] ;
130
+ }
131
+
132
+ /**
133
+ * Assume a proxy configuration specified as:
134
+ * proxy: {
135
+ * 'context': { options }
136
+ * }
137
+ * OR
138
+ * proxy: {
139
+ * 'context': 'target'
140
+ * }
141
+ */
142
+ if ( ! Array . isArray ( options . proxy ) ) {
143
+ options . proxy = Object . keys ( options . proxy ) . map ( function ( context ) {
125
144
var proxyOptions ;
126
- if ( typeof options . proxy [ path ] === 'string' ) {
127
- proxyOptions = { path : path , target : options . proxy [ path ] } ;
128
- } else {
129
- proxyOptions = options . proxy [ path ] ;
130
- proxyOptions . path = path ;
145
+
146
+ if ( typeof options . proxy [ context ] === 'string' ) {
147
+ proxyOptions = {
148
+ context : context ,
149
+ target : options . proxy [ target ]
150
+ } ;
131
151
}
152
+ else {
153
+ proxyOptions = options . proxy [ context ] ;
154
+ proxyOptions . context = context ;
155
+ }
156
+
132
157
return proxyOptions ;
133
158
} ) ;
134
159
}
135
- options . proxy . forEach ( function ( proxyOptions ) {
136
- proxyOptions . ws = proxyOptions . hasOwnProperty ( 'ws' ) ? proxyOptions . ws : true ;
137
- app . all ( proxyOptions . path , function ( req , res , next ) {
138
- var bypassUrl = typeof proxyOptions . bypass === 'function' ? proxyOptions . bypass ( req , res , proxyOptions ) : false ;
139
- if ( bypassUrl ) {
140
- req . url = bypassUrl ;
141
- next ( ) ;
142
- } else {
143
- if ( typeof proxyOptions . rewrite === 'function' ) proxyOptions . rewrite ( req , proxyOptions ) ;
144
- if ( proxyOptions . host ) {
145
- req . headers . host = proxyOptions . host ;
146
- }
147
- proxy . web ( req , res , proxyOptions , function ( err ) {
148
- var msg = "cannot proxy to " + proxyOptions . target + " (" + err . message + ")" ;
149
- this . sockWrite ( this . sockets , "proxy-error" , [ msg ] ) ;
150
- res . statusCode = 502 ;
151
- res . end ( ) ;
152
- } . bind ( this ) ) ;
153
- if ( proxyOptions . configure ) {
154
- proxyOptions . configure ( proxy ) ;
160
+
161
+ /**
162
+ * Assume a proxy configuration specified as:
163
+ * proxy: [
164
+ * {
165
+ * context: ...,
166
+ * ...options...
167
+ * }
168
+ * ]
169
+ */
170
+ options . proxy . forEach ( function ( proxyConfig ) {
171
+ var context = proxyConfig . context || proxyConfig . path ;
172
+
173
+ app . use ( function ( req , res , next ) {
174
+ if ( typeof proxyConfig . bypass === 'function' ) {
175
+ var bypassUrl = proxyConfig . bypass ( req , res , proxyConfig ) || false ;
176
+
177
+ if ( bypassUrl ) {
178
+ req . url = bypassUrl ;
155
179
}
156
180
}
157
- } . bind ( this ) ) ;
158
- } . bind ( this ) ) ;
181
+
182
+ next ( ) ;
183
+ } , httpProxyMiddleware ( context , proxyConfig ) ) ;
184
+ } ) ;
159
185
}
160
186
} . bind ( this ) ,
161
187
@@ -296,7 +322,7 @@ Server.prototype.listen = function() {
296
322
this . sockets . splice ( connIndex , 1 ) ;
297
323
}
298
324
} . bind ( this ) ) ;
299
-
325
+
300
326
if ( this . hot ) this . sockWrite ( [ conn ] , "hot" ) ;
301
327
if ( ! this . _stats ) return ;
302
328
this . _sendStats ( [ conn ] , this . _stats . toJson ( ) , true ) ;
0 commit comments