Code Explainers

Code explainers tagged #middleware

javascript
class ProblemError extends Error {
  constructor(status, title, detail, extensions = {}) {
    super(detail || title);
    this.status = status;

RFC 7807 problem details in Express

error-handling middleware http-api
Intermediate 8 steps
python
from flask import Flask, request, Response
from prometheus_client import Counter, Histogram, generate_latest, CONTENT_TYPE_LATEST
import time
 

Instrumenting a Flask app with Prometheus

metrics observability middleware
Intermediate 6 steps
typescript
import {
  MiddlewareConsumer,
  Module,
  NestModule,

How middleware binds to routes in NestJS

middleware routing modules
Intermediate 6 steps
python
import logging
import uuid
from flask import Flask, request, g
 

Adding correlation IDs to Flask logs

logging request-lifecycle observability
Intermediate 7 steps
javascript
const express = require('express');
const createError = require('http-errors');
const { Article } = require('../models');
 

Sharing route logic with router.param in Express

middleware authorization dry
Intermediate 10 steps
php
<?php
 
namespace App\Http\Middleware;
 

Enforcing upload quotas with Laravel middleware

middleware quota-enforcement collections
Intermediate 8 steps
javascript
const express = require('express');
const cors = require('cors');
 
const app = express();

Configuring a CORS allowlist in Express

cors middleware security
Intermediate 8 steps
rust
use std::sync::Arc;
 
use axum::{
    extract::{FromRef, State},

Substate extraction with FromRef in Axum

state-management routing middleware
Intermediate 8 steps
rust
use axum::{
    async_trait,
    extract::FromRequestParts,
    http::{header::HeaderName, request::Parts, StatusCode},

A custom tenant extractor in Axum

extractors middleware error-handling
Intermediate 7 steps
go
package middleware
 
import (
	"crypto/hmac"

Verifying signed URLs with Gin middleware

hmac middleware authentication
Intermediate 8 steps
rust
use axum::{
    extract::{FromRef, FromRequestParts},
    http::{header, request::Parts, StatusCode},
    RequestPartsExt,

How a JWT extractor works in Axum

jwt authentication extractors
Intermediate 8 steps
go
package server
 
import (
	"net/http"

Rate limiting HTTP handlers with a token bucket

rate-limiting token-bucket middleware
Advanced 7 steps
go
package middleware
 
import (
	"fmt"

How a panic-recovery middleware works in Gin

middleware panic-recovery error-reporting
Intermediate 8 steps
rust
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
 
use axum::body::Body;

A maintenance-mode gate in Axum

middleware shared-state atomics
Intermediate 7 steps
javascript
const express = require('express');
 
const app = express();
 

Enforcing HTTPS with Express middleware

middleware https security
Intermediate 6 steps
ruby
module Rack
  class MaintenanceMode
    RETRY_AFTER = 3600
 

A Rack maintenance-mode middleware in Rails

middleware rack http-status
Intermediate 8 steps
ruby
module RequestTagging
  class Middleware
    def initialize(app)
      @app = app

Per-request context with CurrentAttributes in Rails

middleware thread-safety logging
Intermediate 7 steps
typescript
type Middleware<TIn, TOut> = (ctx: TIn) => Promise<TOut> | TOut;
 
class Pipeline<TIn, TOut> {
  private constructor(private readonly run: Middleware<TIn, TOut>) {}

A type-safe async middleware pipeline

generics type-safety middleware
Advanced 9 steps