Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


How to conditionally proxy request? #167

jch opened this Issue · 4 comments

4 participants


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:

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:, 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'));

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.

At 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:

var http = require('http'),
    httpProxy = require('http-proxy'),
    connect = require('connect');

httpProxy.createServer(function (req, res, proxy) {
  if req.headers['Accept'].match(/custom_api_accept/) {
    var options = {
      host: 'apiserver',
      port: 8080
  } else {
    var options = {
      host: 'localhost',
      port: 8080

  proxy.proxyRequest(req, res, options);

  connect.static(__dirname + '/public')

Didn't test it, but it should give you a hint :)


I'd like to use a URL approach also: handle requests to certain URLs with the primary app, but proxy certain URLs to a separate app. It seems from the examples that the components to do all this exist, but I can't get them to work together. Creating a 3rd server seems redundant... does anyone know another way? Thanks!


@jch does your example not work for you? If you don't call next in your middleware, it won't pass it on to the default handler anyways (next is a function passed into the third parameter in the middleware).


@CodeRarity I ended up with a different architecture, so I no longer needed to proxy requests. It was more trouble than it was worth, so now I have my API and static web endpoints mounted together at the same place.

@jch jch closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.