nodejitsu/node-http-proxy — Issue #167: How to conditionally proxy request?
Author: jch
Created: 2011-12-13 07:29:25+00:00
State: open

I'd like to proxy requests to an API backend if the `Accept` header matches a certain regex. If it doesn't match, no proxying is done and instead a static page is served with Connect's `static` middleware. I saw that `createServer` allows me to either pass in middleware, or to pass in a function to modify before proxying, but I don't see how to conditionally proxy based on request attributes.

How would one go about writing this? In pseudocode, I'd want something like:

```javascript
httpProxy.createServer(function (req, res, proxy) {
if req.headers['Accept'].match(/custom_api_accept/) {
//... modify the request before sending it off to the target
proxy.proxyRequest(req, res, {host: api.host, port: api.port});
// respond with the target's response
} else {
// if Accept doesn't match, then we serve some static assets
var connect = require('connect');
var app = connect();
app.use(connect.static(__dirname + '/public'));
});
}).listen(8000);
```