What is Rack::Cors

Sam Hall
2 min readJun 7, 2021

What is Rack? What is Cors? How do they interact with one another?

“rack-cors” is a gem that you will see and use often. But what is it and what does it do? To know exactly what this gem does, you’ll need some foundational information about both “Rack” and CORS, what they represent and how they work together when implemented in your application.

Rack

“Rack” refers to the foundation of technology, an abstraction, upon which nearly all Ruby frameworks are based. There are a few different components of Rack. One, it is a simple architecture that allows one to build reusable chunks of code that can then be implemented into a larger application, as long as it is compliant with the rules of Rack’s interface. Rack also refers to the gem itself. The Rack gem contains the Rack architecture and allows us to write code within its framework.

Rack Compliant Code

In order for code to comply with the Rack framework it must have the following characteristics:

  1. It must respond to the command “call.”
  2. The “call” method must accept a single argument. The argument goes by env or environment and acts as a bundler for all of the data in the “call” request.

3. The “call” method has to return an array of three things: status, headers and body.

Middleware

The term middleware refers to any software that “acts as a bridge” between a database, or an operating system and the application. Middleware can be used and nested to create larger applications using the Rack pattern and framework.

CORS

“CORS” specifically stands for Cross-Origin Resource Sharing. It is essentially a system of HTTP Headers that transmit signals and determine whether browsers will receive or block frontend code from getting responses for cross-origin requests. CORS functions by adding new headers that let servers determine which origins are permitted to read that information from a given browser. You can do further reading about the specific CORS headers here.

RACK-CORS

When working in concert with one another as the rack-cors gem, the two make up a Middleware that make CORS-compatible Rack-framework applications.

Further Reading

--

--