{"issue":{"user":{"id":19874,"login":"jch","url":"https://api.github.com/users/jch","gravatar_id":"acd4b5803e806bf0ed70299f15cd6d18","avatar_url":"https://secure.gravatar.com/avatar/acd4b5803e806bf0ed70299f15cd6d18?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"milestone":null,"title":"How to conditionally proxy request?","created_at":"2011-12-13T07:29:25Z","labels":[],"state":"open","updated_at":"2011-12-16T06:20:19Z","id":2533717,"html_url":"https://github.com/nodejitsu/node-http-proxy/issues/167","closed_at":null,"pull_request":{"patch_url":null,"html_url":null,"diff_url":null},"number":167,"body":"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.\r\n\r\nHow would one go about writing this?  In pseudocode, I'd want something like:\r\n\r\n```javascript\r\nhttpProxy.createServer(function (req, res, proxy) {\r\n  if req.headers['Accept'].match(/custom_api_accept/) {\r\n    //... modify the request before sending it off to the target\r\n    proxy.proxyRequest(req, res, {host: api.host, port: api.port});\r\n    // respond with the target's response\r\n  } else {\r\n    // if Accept doesn't match, then we serve some static assets\r\n    var connect = require('connect');\r\n    var app = connect();\r\n    app.use(connect.static(__dirname + '/public'));\r\n  });\r\n}).listen(8000);\r\n```","url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/167","comments":1,"assignee":null},"comment":{"user":{"id":681213,"login":"taris","url":"https://api.github.com/users/taris","gravatar_id":"e842fc09517a25b0b2646db2698ade8d","avatar_url":"https://secure.gravatar.com/avatar/e842fc09517a25b0b2646db2698ade8d?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-140.png"},"created_at":"2011-12-16T06:20:19Z","updated_at":"2011-12-16T06:20:19Z","id":3173849,"body":"I did something similar, but based on a url. First i tried it with the static middleware before using the proxy, but it didn't work out the way i wanted.\r\n\r\nAt the end i moved the static part to another connect server instance and proxied to it when my criterias matched. In your case i would do something like that:\r\n\r\n```javascript\r\nvar http = require('http'),\r\n    httpProxy = require('http-proxy'),\r\n    connect = require('connect');\r\n\r\nhttpProxy.createServer(function (req, res, proxy) {\r\n  if req.headers['Accept'].match(/custom_api_accept/) {\r\n    var options = {\r\n      host: 'apiserver',\r\n      port: 8080\r\n    }\r\n  } else {\r\n    var options = {\r\n      host: 'localhost',\r\n      port: 8080\r\n    }\r\n  }\r\n\r\n  proxy.proxyRequest(req, res, options);\r\n}).listen(8000);\r\n\r\nconnect(\r\n  connect.static(__dirname + '/public')\r\n).listen(8080);\r\n```\r\nDidn't test it, but it should give you a hint :)","url":"https://api.github.com/repos/nodejitsu/node-http-proxy/issues/comments/3173849"},"action":"created"}