To add CORS headers to a Ruby HTTP server (such as one built with Rack), you can use the rack-cors
gem. Here's an example of how to add rack-cors
to your application:
Add the rack-cors
gem to your Gemfile and run bundle install
:
main.rb16 chars2 lines
Configure rack-cors
in your application. Here's an example configuration that allows requests from http://example.com
:
main.rb189 chars10 lines
This configuration allows requests from http://example.com
to any endpoint (*
) using any HTTP method.
You can further customize the allow
block to specify more specific URL and HTTP method restrictions.
Start your application and test requests from the allowed origin.
gistlibby LogSnag