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 Middleware is in Ruby on Rails and when it is used

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

  def call(env)
    puts "Retrieved query to #{env['PATH_INFO']}"
    status, heads, body = @app.call(env)
    puts "Returned #{status} status"
    [status, heads, body]
  end
end
In this example, each request will be logged into the console - both upon arrival and after processing.
Middleware is often used for the following tasks:
  • login of requests and responses;
  • caching;
  • error handling;
  • token authentication or verification;
  • compression or modification of the response body;
  • establishing common headings.
In Rails, the middleware chain is defined in the config/application.rb file or viewed by the command:
bin/rails middleware
To add your own middleware, you can paste it into the stack:
config.middleware.use RequestLogger
or between other layers:
config.middleware.insert_before Rack::Runtime, RequestLogger
Middleware helps isolate the technical aspects of query handling - logic 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 modular and as functions
29 Oct 21:53

module_function in Ruby: When module methods are available as modular 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 object type?
30 Oct 19:55

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

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

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

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

The variables in Ruby are @, @@ and class instance variable

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

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

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

What is 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 B-Tree (Balanced Tree)?
22 Nov 12:58

What is B-Tree (Balanced Tree)?

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