/Knowledge/sources/honojs__hono/llms-full/middleware-custom-middleware--35b220049c2a.md

Middleware: Custom Middleware - hono-docs

Middleware: Custom Middleware Source evidence: /Sources/honojs hono/provenance.md Canonical citation: https://hono.dev/llms full.txt custom middleware Summar...

Middleware: Custom Middleware

Source evidence: /Sources/honojs__hono/provenance.md Canonical citation: https://hono.dev/llms-full.txt#custom-middleware

Summary

You can write your own middleware directly inside app.use(): ```ts // Custom logger app.use(async (c, next) => { console.log([${c.req.method}] ${c.req.url}) await next() }) // Add a custom header app.use('/message/*', async (c, next)...

Content

You can write your own middleware directly inside app.use():

// Custom logger
app.use(async (c, next) => {
  console.log(`[${c.req.method}] ${c.req.url}`)
  await next()
})

// Add a custom header
app.use('/message/*', async (c, next) => {
  await next()
  c.header('x-message', 'This is middleware!')
})

app.get('/message/hello', (c) => c.text('Hello Middleware!'))

However, embedding middleware directly within app.use() can limit its reusability. Therefore, we can separate our middleware into different files.

To ensure we don't lose type definitions for context and next, when separating middleware, we can use createMiddleware() from Hono's factory. This also allows us to type-safely access data we've set in Context from downstream handlers.

Metadata

{
  "chunk_index": 0,
  "citation": "https://hono.dev/llms-full.txt#custom-middleware",
  "coverage_role": "overview",
  "qualified_title": "Hono / llms-full / Middleware > Custom Middleware (1)",
  "retrieved_at": "2026-07-02T05:51:01Z",
  "section_index": 115,
  "source_id": "/honojs/hono",
  "source_type": "llms_full",
  "target_label": "llms-full",
  "title": "Middleware: Custom Middleware",
  "upstream_url": "https://hono.dev/llms-full.txt",
  "version": "2026"
}
Kinic Wiki
Loading knowledge node
Details

Identity

database
db_23dhmsxlhukv
database_id
db_23dhmsxlhukv
path
/Knowledge/sources/honojs__hono/llms-full/middleware-custom-middleware--35b220049c2a.md
kind
directory
role
markdown_note
children
0

Metadata

created_at
virtual
updated_at
virtual
etag
virtual
metadata_json
{}

Lint Hints

No lightweight warnings.

Outgoing Links

No outgoing links indexed.

Incoming Links

Select a file node to inspect backlinks.

Raw Source

No raw source path inferred.