是的,我不完全确定您要做什么。但是你可以这样做
class CorsWired
def initialize(app)
@app = app
end
def call(env)
cors = Rack::Cors.new(@app, {}) do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :options, :delete], :credentials => false
end
end
cors.call(env)
end
end
您的 config.ru 应该有 use CorsWired,而不是 use CorsWired.new
这就是我想你要问的,但我认为你错过了中间件的意义。您应该根据您的需要更改您的 config.ru 以在中间件之前/之后使用 rack-cors。
require 'rack'
require 'rack/cors'
require './cors_wired'
app = Rack::Builder.new do
use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :options, :delete], :credentials => false
end
end
use CorsWired
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['OK']] }
end
run app