LearnBoost/distribute — Issue #1: httpProxy.createServer(middlewares)
Author: indexzero
Created: 2012-01-22 01:35:54+00:00
State: open

`node-http-proxy` essentially exposes three core pieces of functionality:

1. `httpProxy.HttpProxy`: The core reverse-proxy logic responsible for proxying a request to a specific target
2. `httpProxy.RoutingProxy`: Reverse-proxies a request to multiple targets lazy-filling a set of `HttpProxy` instances.
3. `httpProxy.createServer`: Method which takes various arguments (https://github.com/nodejitsu/node-http-proxy/blob/master/lib/node-http-proxy.js#L85-114).

One of the kinds of arguments which can be passed to `httpProxy.createServer()` is a set of middlewares. @dominictarr wrote this and used a modified version of `stack` from @creationix: https://github.com/nodejitsu/node-http-proxy/blob/master/lib/node-http-proxy.js#L278-327

The reason it is "adapted" is that we set the prototype of the `next()` function to be the instance of the proxy itself. In this way you can:

**Perform a proxy operation**
``` js
httpProxy.createServer(
// ... other middlewares
function (req, res, next) {
next.proxyRequest(req, res, { host: 'http://whatever.com', port: 80 });
}
);
```

**Pass-thru by invoking next**
``` js
httpProxy.createServer(
// ... other middlewares
function (req, res, next) {
next();
}
);
```

I like a lot of the distribute APIs, would you be open to combining forces and rolling it into `node-http-proxy`?