Skip to content

Commit

Permalink
fix(Express Router): non proxy routes not working
Browse files Browse the repository at this point in the history
  • Loading branch information
chimurai committed Aug 7, 2016
1 parent 306fdd4 commit 2d5dff4
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions lib/index.js
Expand Up @@ -35,10 +35,7 @@ function HttpProxyMiddleware(context, opts) {
return middleware;

function middleware(req, res, next) {
// https://github.com/chimurai/http-proxy-middleware/issues/17
req.url = req.originalUrl;

if (contextMatcher.match(config.context, req.url, req)) {
if (shouldProxy(config.context, req)) {
var activeProxyOptions = prepareProxyRequest(req);
proxy.web(req, res, activeProxyOptions);
} else {
Expand All @@ -55,20 +52,35 @@ function HttpProxyMiddleware(context, opts) {
}

function handleUpgrade(req, socket, head) {
if (contextMatcher.match(config.context, req.url, req)) {
if (shouldProxy(config.context, req)) {
var activeProxyOptions = prepareProxyRequest(req);
proxy.ws(req, socket, head, activeProxyOptions);
logger.info('[HPM] Upgrading to WebSocket');
}
}

/**
* Determine wether request should be proxied.
*
* @private
* @return {Boolean}
*/
function shouldProxy(context, req) {
var path = (req.originalUrl || req.url);
return contextMatcher.match(context, path, req);
}

/**
* Apply option.router and option.pathRewrite
* Order matters:
Router uses original path for routing;
NOT the modified path, after it has been rewritten by pathRewrite
*/
function prepareProxyRequest(req) {
// https://github.com/chimurai/http-proxy-middleware/issues/17
// https://github.com/chimurai/http-proxy-middleware/issues/94
req.url = (req.originalUrl || req.url);

// store uri before it gets rewritten for logging
var originalPath = req.url;
var newProxyOptions = _.cloneDeep(proxyOptions);
Expand Down

0 comments on commit 2d5dff4

Please sign in to comment.