In Sinatra, is there a way to forward a request to another app?

霸气de小男生 提交于 2019-12-11 15:36:25

问题


I'm trying to do something like this:

class Dispatcher < Sinatra::Base
  def initialize
    @foo = Foo.new
  end

  get '/foo/*' do 
    @foo.call(params[:splat])
  end
end

So that URL /foo/abc/def?xxx=yyy would be like calling the Foo app with /abc/def?xxx=yyy.

This seems like it should be easy, but I haven't seen any example of it.


回答1:


I ended up doing this in a Rack config.ru file:

map "/abc" do
  run Foo.new('abc')
end

map "/def" do
  run Foo.new('def')
end

Not exactly what I wanted, but saves me from modifying the underlying app.




回答2:


I'm not sure why you would use Sinatra for that. If I understand you right, and you use Apache with Proxy-Rewrite rules you just do:

The .htaccess file

RewriteEngine On
RewriteRule ^foo/(.*) http://localhost:61000/$1 [P]

So all your domain.tdl/foo get redirected to your local running app athttp://localhost:61000/ with all Post and Get Parameters.



来源:https://stackoverflow.com/questions/19546886/in-sinatra-is-there-a-way-to-forward-a-request-to-another-app

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!