In a server-side web application framework, it is the middleware that handles the HTTP request/response processing pipeline to handle server-side tasks. By the word "middle" we can say that the middleware is the mid-layer between the HTTP request towards the application logic. One of the main functions of the middleware is filtering HTTP requests from the user's browser before the application logic.

Oftentimes, there are multiple middleware layers in an application. The request goes through each middleware layer, and on each middleware layer, there is different logic implemented. Each middleware layer contains access to the HTTP request and can either pass the HTTP request to the next middleware layer of the application or can just return the response. Returning the response prevents the HTTPS request to be passed to the following middleware layers.

The following are the typical usages of middleware:

Logging
Rate-limiting incoming HTTP requests
User authorization
Handling CORS

Logging

1. As mentioned earlier, the middleware receives the HTTP requests, and with those HTTP requests are additional information like client IP address, payload, and HTTP headers. From that information, a developer can create a logging function.

Rate-limiting incoming HTTP requests

2. It is in the middleware that it is an excellent idea to create a PHP logic to limit how often a request from a single IP address. This is to avoid spam or DDoS attacks.

User authorization

3. Once a user is authenticated, usually it is in the middleware that decides if the user's role can access this specific page or not.

Handling CORS

4. To manage CORS, you can setup CORS middleware to accept specific origins and headers. The middleware is a suitable component where you can enforce the checking of HTTP headers that correlate to CORS.

As you see from these examples, middleware is a very critical component of a web application. Different logic or functions that the application needs, can be inserted to the middleware. It is most important for a developer to learn and know how to implement a middleware for web server applications.

Here is the step by step process on how to create a middleware on Drupal

1. Create your custom module
2. Create a PHP file on the following directory module_name/src/CustomMiddleware.php
<?php

namespace Drupal\mymodule;

use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * FirstMiddleware middleware.
 */
class CustomMiddleware implements HttpKernelInterface {

    use StringTranslationTrait;

    /**
     * The kernel.
     *
     * @var \Symfony\Component\HttpKernel\HttpKernelInterface
     */
    protected $httpKernel;

    /**
     * Constructs the FirstMiddleware object.
     *
     * @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
     *   The decorated kernel.
     */
    public function __construct(HttpKernelInterface $http_kernel) {
        $this->httpKernel = $http_kernel;
    }

    /**
     * {@inheritdoc}
     */
    public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = TRUE) {
        if ($request->getClientIp() == '127.0.0.1') {
            return new Response($this->t('hello world!'), 403);
        }
        return $this->httpKernel->handle($request, $type, $catch);
    }

}
3. Create a YML file on the following directory module_name/module_name.services.yml

services:
  mymodule.custom_middleware:
    class: Drupal\mymodule\CustomMiddleware
    tags:
      - { name: http_middleware, priority: 150 }