@@ -58,19 +58,10 @@ var httpProxyMiddleware = function (context, opts) {
58
58
if ( contextMatcher . match ( config . context , req . url ) ) {
59
59
logger . debug ( '[HPM] Context match: "%s" -> "%s"' , config . context , req . url ) ;
60
60
61
- // handle option.pathRewrite
62
- if ( pathRewriter ) {
63
- req . url = pathRewriter ( req . url ) ;
64
- }
65
- if ( proxyOptions . proxyTable ) {
66
- // change option.target when proxyTable present.
67
- var altOpts = ProxyTable . createProxyOptions ( req , proxyOptions ) ;
68
- logger . debug ( '[HPM] Proxying "%s": "%s" -> "%s"' , req . url , req . hostname , altOpts . target ) ;
69
- proxy . web ( req , res , altOpts ) ;
70
- } else {
71
- logger . debug ( '[HPM] Proxying "%s": "%s" -> "%s"' , req . url , req . hostname , proxyOptions . target ) ;
72
- proxy . web ( req , res ) ;
73
- }
61
+ var activeProxyOptions = __prepareProxyRequest ( req ) ;
62
+
63
+ logger . debug ( '[HPM] Proxying "%s": "%s" -> "%s"' , req . url , req . hostname , activeProxyOptions . target ) ;
64
+ proxy . web ( req , res , activeProxyOptions ) ;
74
65
75
66
} else {
76
67
next ( ) ;
@@ -93,14 +84,49 @@ var httpProxyMiddleware = function (context, opts) {
93
84
94
85
function handleUpgrade ( req , socket , head ) {
95
86
if ( contextMatcher . match ( config . context , req . url ) ) {
96
- if ( pathRewriter ) {
97
- req . url = pathRewriter ( req . url ) ;
98
- }
99
- proxy . ws ( req , socket , head ) ;
87
+
88
+ var activeProxyOptions = __prepareProxyRequest ( req ) ;
89
+
90
+ proxy . ws ( req , socket , head , activeProxyOptions ) ;
100
91
logger . info ( '[HPM] Upgrading to WebSocket' ) ;
101
92
}
102
93
}
103
94
95
+ /**
96
+ * Apply option.proxyTable and option.pathRewrite
97
+ * Order matters:
98
+ ProxyTable uses original path for routing;
99
+ NOT the modified path, after it has been rewritten by pathRewrite
100
+ */
101
+ function __prepareProxyRequest ( req ) {
102
+ // apply option.proxyTable
103
+ var alteredProxyOptions = __applyProxyTableOption ( req , proxyOptions ) ;
104
+
105
+ // apply option.pathRewrite
106
+ __applyPathRewrite ( req , pathRewriter ) ;
107
+
108
+ return alteredProxyOptions ;
109
+ }
110
+
111
+ // Modify option.target when proxyTable present.
112
+ // return altered options
113
+ function __applyProxyTableOption ( req ) {
114
+ var result = proxyOptions ;
115
+
116
+ if ( proxyOptions . proxyTable ) {
117
+ result = ProxyTable . createProxyOptions ( req , proxyOptions ) ;
118
+ }
119
+
120
+ return result ;
121
+ }
122
+
123
+ // rewrite path
124
+ function __applyPathRewrite ( req ) {
125
+ if ( pathRewriter ) {
126
+ req . url = pathRewriter ( req . url ) ;
127
+ }
128
+ }
129
+
104
130
function getProxyErrorHandler ( ) {
105
131
if ( _ . isFunction ( proxyOptions . onError ) ) {
106
132
return proxyOptions . onError ; // custom error listener
@@ -111,7 +137,7 @@ var httpProxyMiddleware = function (context, opts) {
111
137
112
138
function proxyErrorLogger ( err , req , res ) {
113
139
var hostname = ( req . hostname || req . host ) || ( req . headers && req . headers . host ) // (node0.10 || node 4/5) || (websocket)
114
- var targetUri = proxyOptions . target . host + req . url ;
140
+ var targetUri = ( proxyOptions . target . host || proxyOptions . target ) + req . url ;
115
141
116
142
logger . error ( '[HPM] Proxy error: %s. %s -> "%s"' , err . code , hostname , targetUri ) ;
117
143
}
0 commit comments