2012-01-10 27 views
2

我正在使用rake-pipeline如here所述爲Ember.js應用設置我的devel環境。使用rake-pipeline-web-filters進行ajax調用的代理

在開發過程中,我的HTML和JavaScript通過使用WEBrick提供服務(耙過濾神奇,我也不太明白)上http://0.0.0.0:9292和我有阿帕奇提供php開發上http://somename.local

我的AJAX REST服務由於瀏覽器的反跨域AJAX事情,來自客戶端應用程序的調用正在丟失。我該如何解決這個問題?

回答

2

您不能直接在您的資產文件中配置代理。您必須創建一個config.ru文件並使用rackup命令啓動服務器。

下面是一個例子Assetfile:

input "app" 
output "public" 

而且config.ru:

require 'rake-pipeline' 
require 'rake-pipeline/middleware' 
require "rack/streaming_proxy" # Don't forget to install the rack-streaming-proxy gem. 

use Rack::StreamingProxy do |request| 
    # Insert your own logic here 
    if request.path.start_with?("/api") 
    "http://localhost#{request.path.sub("/api", "")}" 
    end 
end 

use Rake::Pipeline::Middleware, 'Assetfile' # This is the path to your Assetfile 
run Rack::Directory.new('public') # This should match whatever your Assetfile's output directory is 

你必須安裝機架和機架流代理的寶石。

1

您可以使用Rack::Proxy,然後將所需的請求發送到代理。

if request.path.start_with?("/api") 
    URI.parse("http://localhost:80#{request.path}") 
    end 
+0

謝謝托馬斯,你能再詳細一點嗎?我有一個像這樣的資產文件https://gist.github.com/1608930我在哪裏添加Rack :: Proxy代碼? – 2012-01-13 21:59:14

+0

這是如何傳遞方法,POST,DELETE或PUT的?它看起來像只是將每個方法轉換爲GET。 – Lucas 2012-09-06 22:20:56