Code Explainers

Code explainers tagged #middleware

go
package middleware
 
import (
	"crypto/subtle"

Building an API-key middleware in Gin

middleware authentication dependency-injection
Intermediate 5 steps
go
package api
 
func RegisterRoutes(r *gin.Engine, deps *Dependencies) {
	r.GET("/health", func(c *gin.Context) {

Layering route groups and middleware in Gin

routing middleware authentication
Intermediate 8 steps
javascript
const express = require('express');
const compression = require('compression');
const zlib = require('zlib');
 

Tuning Express response compression

compression middleware http
Intermediate 9 steps
javascript
const logger = require('./logger');
 
class AppError extends Error {
  constructor(message, statusCode = 500, details = null) {

Centralized error handling in Express

error-handling middleware custom-errors
Intermediate 8 steps
go
package middleware
 
import (
	"compress/gzip"

How gzip response compression works in Gin

middleware compression http
Intermediate 8 steps
go
package middleware
 
import (
	"context"

Correlation ID tracing middleware in Gin

middleware request tracing structured logging
Intermediate 7 steps
rust
use std::sync::Arc;
use axum::{
    body::Body,
    extract::{Extension, State},

Passing per-request context through Axum middleware

middleware request-context dependency-injection
Intermediate 8 steps
javascript
const crypto = require('crypto');
 
function securityHeaders(options = {}) {
  const {

A configurable security-headers middleware in Express

middleware http-headers content-security-policy
Intermediate 7 steps
php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\ReportController;

How nested route groups compose in Laravel

routing middleware authorization
Intermediate 8 steps
go
package middleware
 
import (
	"bytes"

Verifying webhook HMAC signatures in Gin

hmac middleware webhooks
Intermediate 7 steps
go
package middleware
 
import (
	"time"

Building a request logger middleware in Gin

middleware structured-logging closures
Intermediate 6 steps
rust
use axum::{
    body::Body,
    extract::Request,
    http::{header, StatusCode},

How bearer-auth middleware works in Axum

middleware authentication request extensions
Intermediate 7 steps
go
package auth
 
import (
	"net/http"

Building a JWT auth middleware in Gin

middleware jwt authentication
Intermediate 7 steps
javascript
const { validationResult, matchedData } = require('express-validator');
 
function validate(schema) {
  const runners = schema.map((rule) => rule.run.bind(rule));

A reusable validation middleware in Express

middleware validation higher-order-functions
Intermediate 8 steps
rust
use axum::{
    http::StatusCode,
    response::IntoResponse,
    routing::get,

Serving an SPA and API with Axum

routing static-files spa-fallback
Intermediate 7 steps
go
func RecoveryHandler(logger *zap.Logger) gin.HandlerFunc {
	return gin.CustomRecovery(func(c *gin.Context, recovered any) {
		var brokenPipe bool
		if ne, ok := recovered.(*net.OpError); ok {

A panic-recovery middleware in Gin

middleware panic-recovery structured-logging
Intermediate 6 steps
go
package server
 
import (
	"context"

Composing HTTP middleware in Go

middleware http-handlers closures
Intermediate 8 steps
javascript
const asyncHandler = (fn) => (req, res, next) => {
  Promise.resolve(fn(req, res, next)).catch(next);
};
 

Async error handling in Express routes

async-await error-handling middleware
Intermediate 7 steps