http-proxy-middleware
Advanced tools
Comparing version 0.9.1 to 0.10.0-beta
62
index.js
@@ -61,16 +61,7 @@ var _ = require('lodash'); | ||
// handle option.pathRewrite | ||
if (pathRewriter) { | ||
req.url = pathRewriter(req.url); | ||
} | ||
if (proxyOptions.proxyTable) { | ||
// change option.target when proxyTable present. | ||
var altOpts = ProxyTable.createProxyOptions(req, proxyOptions); | ||
logger.debug('[HPM] Proxying "%s": "%s" -> "%s"', req.url, req.hostname, altOpts.target); | ||
proxy.web(req, res, altOpts); | ||
} else { | ||
logger.debug('[HPM] Proxying "%s": "%s" -> "%s"', req.url, req.hostname, proxyOptions.target); | ||
proxy.web(req, res); | ||
} | ||
var activeProxyOptions = __prepareProxyRequest(req); | ||
logger.debug('[HPM] Proxying "%s": "%s" -> "%s"', req.url, req.hostname, activeProxyOptions.target); | ||
proxy.web(req, res, activeProxyOptions); | ||
} else { | ||
@@ -97,6 +88,6 @@ next(); | ||
if (contextMatcher.match(config.context, req.url)) { | ||
if (pathRewriter) { | ||
req.url = pathRewriter(req.url); | ||
} | ||
proxy.ws(req, socket, head); | ||
var activeProxyOptions = __prepareProxyRequest(req); | ||
proxy.ws(req, socket, head, activeProxyOptions); | ||
logger.info('[HPM] Upgrading to WebSocket'); | ||
@@ -106,2 +97,37 @@ } | ||
/** | ||
* Apply option.proxyTable and option.pathRewrite | ||
* Order matters: | ||
ProxyTable uses original path for routing; | ||
NOT the modified path, after it has been rewritten by pathRewrite | ||
*/ | ||
function __prepareProxyRequest(req) { | ||
// apply option.proxyTable | ||
var alteredProxyOptions = __applyProxyTableOption(req, proxyOptions); | ||
// apply option.pathRewrite | ||
__applyPathRewrite(req, pathRewriter); | ||
return alteredProxyOptions; | ||
} | ||
// Modify option.target when proxyTable present. | ||
// return altered options | ||
function __applyProxyTableOption (req) { | ||
var result = proxyOptions; | ||
if (proxyOptions.proxyTable) { | ||
result = ProxyTable.createProxyOptions(req, proxyOptions); | ||
} | ||
return result; | ||
} | ||
// rewrite path | ||
function __applyPathRewrite (req) { | ||
if (pathRewriter) { | ||
req.url = pathRewriter(req.url); | ||
} | ||
} | ||
function getProxyErrorHandler () { | ||
@@ -117,3 +143,3 @@ if (_.isFunction(proxyOptions.onError)) { | ||
var hostname = (req.hostname || req.host) || (req.headers && req.headers.host) // (node0.10 || node 4/5) || (websocket) | ||
var targetUri = proxyOptions.target.host + req.url; | ||
var targetUri = (proxyOptions.target.host || proxyOptions.target) + req.url; | ||
@@ -120,0 +146,0 @@ logger.error('[HPM] Proxy error: %s. %s -> "%s"', err.code, hostname, targetUri); |
{ | ||
"name": "http-proxy-middleware", | ||
"version": "0.9.1", | ||
"version": "0.10.0-beta", | ||
"description": "The one-liner node.js proxy middleware for connect, express and browser-sync", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -109,2 +109,31 @@ var expect = require('chai').expect; | ||
describe('with proxyTable and pathRewrite', function () { | ||
beforeEach(function () { | ||
proxyServer.close(); | ||
// override | ||
proxy = proxyMiddleware('ws://notworkinghost:6789', {proxyTable: {'/socket': 'ws://localhost:8000'}, pathRewrite: {'^/socket' : ''}}); | ||
proxyServer = createServer(3000, proxy); | ||
}); | ||
beforeEach(function (done) { | ||
proxyServer.on('upgrade', proxy.upgrade); | ||
ws = new WebSocket('ws://localhost:3000/socket'); | ||
ws.on('message', function incoming(message) { | ||
responseMessage = message; | ||
done(); | ||
}); | ||
ws.on('open', function open() { | ||
ws.send('foobar'); | ||
}); | ||
}); | ||
it('should proxy to path', function () { | ||
expect(responseMessage).to.equal('foobar'); | ||
}); | ||
}); | ||
afterEach(function () { | ||
@@ -115,3 +144,2 @@ proxyServer.close(); | ||
}); | ||
}); | ||
@@ -118,0 +146,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119576
1943
54