Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I proxy AJAX requests with Rack Middleware?

I'm developing a Rails application that uses an API backend for AJAX requests written with Sinatra.

The API runs separately from Rails:

Rails: localhost:3000
API: localhost:9393

In production, we'll be proxying requests to the API with nginx.

The problem is that we don't have nginx in development mode, we're using thin. So I need some sort of Rack middleware that I can add in development mode to proxy the requests for me.

Can someone give me an example of how to do this?

like image 674
Adam Lassek Avatar asked Jan 10 '12 22:01

Adam Lassek


1 Answers

Perhaps Rack::Proxy:

http://coderack.org/users/cwninja/middlewares/18-rackproxy

use Rack::Proxy do |req|
  if req.path =~ %r{identify api request with regex here}
    URI.parse("http://localhost:9393/#{req.fullpath}")
  end
end
like image 79
minikomi Avatar answered Oct 03 '22 00:10

minikomi