|
26 | 26 |
|
27 | 27 | var util = require('util'),
|
28 | 28 | events = require('events'),
|
29 |
| - fs = require('fs'); |
| 29 | + fs = require('fs'), |
| 30 | + url = require('url'); |
30 | 31 |
|
31 | 32 | //
|
32 | 33 | // ### function ProxyTable (router, silent)
|
@@ -137,17 +138,17 @@ ProxyTable.prototype.getProxyLocation = function (req) {
|
137 | 138 | for (var i in this.routes) {
|
138 | 139 | var route = this.routes[i];
|
139 | 140 | if (target.match(route.route)) {
|
140 |
| - var pathSegments = route.path.split('/'); |
141 |
| - |
142 |
| - if (pathSegments.length > 1) { |
143 |
| - // don't include the proxytable path segments in the proxied request url |
144 |
| - pathSegments = new RegExp("/" + pathSegments.slice(1).join('/')); |
145 |
| - req.url = req.url.replace(pathSegments, ''); |
146 |
| - } |
147 |
| - |
148 |
| - var location = route.target.split(':'), |
149 |
| - host = location[0], |
150 |
| - port = location.length === 1 ? 80 : location[1]; |
| 141 | + var requrl = url.parse(req.url); |
| 142 | + //add the 'http://'' to get around a url.parse bug, it won't actually be used. |
| 143 | + var targeturl = url.parse('http://'+route.target); |
| 144 | + var pathurl = url.parse('http://'+route.path); |
| 145 | + |
| 146 | + //This replaces the path's part of the URL to the target's part of the URL. |
| 147 | + requrl.pathname = requrl.pathname.replace(pathurl.pathname, targeturl.pathname); |
| 148 | + req.url = url.format(requrl); |
| 149 | + |
| 150 | + var host = targeturl.hostname, |
| 151 | + port = targeturl.port || 80; |
151 | 152 |
|
152 | 153 | return {
|
153 | 154 | port: port,
|
|
0 commit comments