All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is Middleware in Ruby on Rails and when is it used

Post cover: What is Middleware in Ruby on Rails and when is it used
This content has been automatically translated from Ukrainian.
Middleware in Ruby on Rails is an intermediary layer between the client request and the web application. It receives the request before it reaches the controller and can modify or process it. Similarly, Middleware can intercept the response from the application before it is sent to the client.
Rails is built on top of the Rack library, which defines a standard for such middleware layers. Middleware in this context is a regular Ruby class that implements the call(env) method. The env parameter contains all the information about the HTTP request (for example, path, headers, method). The method must return an array of three elements: response status, headers, and body.
The simplest example of custom Middleware might look like this:
class RequestLogger
  def initialize(app)
    @app = app
  end

  def call(env)
    puts "Received request to #{env['PATH_INFO']}"
    status, headers, body = @app.call(env)
    puts "Returned status #{status}"
    [status, headers, body]
  end
end
In this example, each request will be logged to the console - both upon arrival and after processing.
Middleware is often used for tasks such as:
  • logging requests and responses;
  • caching;
  • error handling;
  • authentication or token verification;
  • compression or modification of the response body;
  • setting common headers.
In Rails, the middleware chain is defined in the config/application.rb file or can be viewed with the command:
bin/rails middleware
To add custom middleware, it can be inserted into the stack:
config.middleware.use RequestLogger
or between other layers:
config.middleware.insert_before Rack::Runtime, RequestLogger
Middleware helps isolate technical aspects of request processing - logic that is not directly related to business code. This makes the application cleaner, more extensible, and easier to maintain.

This post doesn't have any additions from the author yet.

module_function in Ruby: when module methods are available as module methods and as functions
29 Oct 21:53

module_function in Ruby: when module methods are available as module methods and as functions

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is memoization in Ruby?
30 Oct 10:17

What is memoization in Ruby?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?
30 Oct 19:55

is_a?, kind_of?, instance_of? — how does Ruby check the type of an object?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
&& vs and — the difference in Ruby that can break your code
30 Oct 20:23

&& vs and — the difference in Ruby that can break your code

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
Variables in Ruby: @, @@ and class instance variable
30 Oct 20:54

Variables in Ruby: @, @@ and class instance variable

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
The difference between blank?, present?, empty? and nil? in Ruby
30 Oct 21:06

The difference between blank?, present?, empty? and nil? in Ruby

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is the Vanilla Rails approach?
14 Nov 16:48

What is the Vanilla Rails approach?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is Elasticsearch and how does it work?
22 Nov 12:35

What is Elasticsearch and how does it work?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a time-series database?
22 Nov 12:42

What is a time-series database?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
22 Nov 12:49

What is VACUUM in PostgreSQL?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska
What is a B-Tree (Balanced Tree)?
22 Nov 12:58

What is a B-Tree (Balanced Tree)?

Нотатки про Ruby та RoR
Нотатки про Ruby та RoR@kovbaska