14

I'm trying to redirect traffic from my test app's /api/* url to my api hosted on Heroku.

Therefore, localhost/api/hello should be proxied to testapp.heroku.com/hello and the response returned.

Using node-http-proxy works perfectly on localhost to localhost, but when I point it to myapp.heroku.com, I get this error:

Heroku | No such app
There is no app configured at that hostname.
Perhaps the app owner has renamed it, or you mistyped the URL.

I have a feeling it's Heroku's routing system that's fudging up my proxied request, and I haven't found a way to fix it. Any ideas?

Le_isms
  • 407
  • 1
  • 4
  • 10

1 Answers1

18

I have seen something similar when proxing requests to a different domain. The work around that I used was to modify the host header on the proxy request to match the domain name the remote site expects. So in your case the code would look like:

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


var server = httpProxy.createServer(function (req, res, proxy) {
  req.headers.host = 'myapp.heroku.com';
  proxy.proxyRequest(req, res, {
    port: 80,
    host: 'myapp.heroku.com'
  });
}).listen(9000);

I'd be interested to know if this works for you.

Derrish Repchick
  • 1,049
  • 8
  • 5
  • It works flawlessly. Setting the host in the header fixed it. For some reason I thought http-proxy would've done that. Thanks for saving me from using a dirty cURL hack!! Thanks! – Le_isms Jun 22 '11 at 20:30
  • 3
    http-proxy supports a changeOrigin option that takes care of this. https://github.com/nodejitsu/node-http-proxy#http-proxy –  Oct 27 '12 at 03:38
  • I literally discovered and did the exact same thing. – Christian Bongiorno Sep 04 '15 at 23:14