---
title: "Utility Middleware"
description: "Utility middleware provides standard tools to enhance your application's request handling, such as logging traffic, compressing responses, and managing request payloads. These components are design..."
last_updated: "2026-07-02T09:15:05.274326+00:00"
canonical_url: "https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/middleware/utility-middleware"
---

Utility middleware provides standard tools to enhance your application's request handling, such as logging traffic, compressing responses, and managing request payloads. These components are designed to be "plug-and-play," allowing you to add functionality to your routes with minimal configuration.

## Logging Middleware
The `logger` middleware prints request and response details to your console, including the HTTP method, request path, status code, and response time.

**Usage:**
```typescript
import { logger } from 'hono/logger'

const app = new Hono()

app.use(logger())
```

You can customize the output by passing a printing function (default is `console.log`):
```typescript
app.use(logger((message) => {
  // Custom logging logic (e.g., sending to a service)
  console.log(`[LOG]: ${message}`)
}))
```

## Body Limit Middleware
The `bodyLimit` middleware prevents your server from processing requests that are too large, helping to protect against memory exhaustion or malicious uploads.

**Usage:**
```typescript
import { bodyLimit } from 'hono/body-limit'

app.post(
  '/upload',
  bodyLimit({
    maxSize: 50 * 1024, // 50kb
    onError: (c) => c.text('Payload too large', 413),
  }),
  async (c) => {
    // Handler logic...
  }
)
```

> [!WARNING]
> If a request exceeds the `maxSize`, the middleware will trigger the `onError` handler (or throw a 413 error by default) and stop the request from reaching your route handler.

## Compression Middleware
The `compress` middleware automatically shrinks your response body using `gzip` or `deflate` algorithms, which can significantly improve performance for text-based content.

**Usage:**
```typescript
import { compress } from 'hono/compress'

const app = new Hono()
app.use(compress())
```

| Option | Default | Description |
| :--- | :--- | :--- |
| `encoding` | undefined | Force 'gzip' or 'deflate' (defaults to negotiation). |
| `threshold` | 1024 | Minimum size in bytes to trigger compression. |
| `contentTypeFilter` | RegExp | Filter which content types should be compressed. |

> [!TIP]
> The compression middleware automatically transforms strong ETags into weak ETags (`W/`), as compressed data is not byte-identical to the original uncompressed source.

## Key Concepts
*   **Middleware:** Functions that run during the request-response lifecycle. They can perform actions, modify the request/response, or stop the chain entirely.
*   **Next:** The `next()` function is called to pass control to the subsequent middleware or route handler in the stack.
*   **Payload:** The actual data sent in an HTTP request (the "body").
*   **Chunked Encoding:** A way of streaming data in parts, often used when the total size is unknown upfront. The `bodyLimit` middleware handles these cases by buffering and measuring the incoming data.

> [!NOTE]
> The `compress` middleware will not compress responses if the `Cache-Control` header contains `no-transform`, following standard HTTP best practices.

## Related

- [Authentication and Security](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/guide/middleware/authentication-and-security)


## Sitemap

See the full [sitemap](https://www.doc0.app/docs/552ca36e-f67e-41c3-a07a-def9bd9551b0/llms.txt) for all pages in this wiki.
