Understanding Hono route and middleware registration order
In Hono, register shared middleware with app.use() and path handlers with methods such as app.get(). Handlers run in registration order, and a handler that returns a response ends request processing. Put shared middleware before routes and fallback behavior after specific routes. This article follows the current Hono routing and middleware APIs to build a health endpoint and a custom 404 response.
In Hono, register shared middleware with app.use() and path handlers with methods such as app.get(). Handlers run in registration order, and a handler that returns a response ends request processing. Put shared middleware before routes and fallback behavior after specific routes. This article follows the current Hono routing and middleware APIs to build a health endpoint and a custom 404 response.
Add a shared response header and a route
Hono is a TypeScript/JavaScript web framework built on Web APIs and supports multiple JavaScript runtimes. This example does not start a runtime server; it uses Hono's request() helper to inspect an app response. For deployment, follow the adapter and startup steps for the chosen runtime, such as Node.js, Bun, Deno, or Workers.
import { Hono } from 'hono'
const app = new Hono()
app.use('/health', async (c, next) => {
await next()
c.header('X-Service', 'status-api')
})
app.get('/health', (c) => c.json({ status: 'ok' }))
app.notFound((c) => c.json({ error: 'Not Found' }, 404))
const response = await app.request('/health')
console.log(response.status) // 200
console.log(response.headers.get('X-Service')) // status-apiRegistration order controls behavior
app.use('/health', ...) registers middleware for that path. After await next() passes to the next handler and returns, the middleware adds X-Service. This lets middleware adjust a response after its handler creates it. If middleware does not await next() or returns a Response to exit early, later processing will not run.
Route order also matters. Hono's examples show handlers for a method running in registration order; when a handler responds, processing stops. A broad path or fallback registered first can prevent a later specific route from being reached. Register shared logging or authentication middleware first, then specific routes, and finally fallback behavior.
Keep 404 and error handling distinct
app.notFound() customizes the response when no route matches. It is separate from app.onError(), which handles uncaught exceptions and can return a custom Response. Hono documents that route-level error handlers can take priority over a parent app's handler. When mounting or composing apps, check which app's settings apply.
Verify the expected behavior
In a project with Hono and a TypeScript execution or test setup, place the example in an app module and use the documented app.request('/health') helper to create a request. Expected results are status 200, JSON {"status":"ok"}, and X-Service: status-api. An unknown path should get the custom 404 body. The code was not run here; support for top-level await depends on the project and runtime.
If the header is missing, check that the middleware path matches and that code after await next() is reachable. If the 404 differs, check whether a broad route or fallback was registered before the specific route. If startup fails, inspect the Hono route code separately from the runtime adapter configuration.
Official sources
[Hono repository and supported runtimes](https://github.com/honojs/hono)
[Hono middleware guide](https://hono.dev/docs/guides/middleware)
[Hono routing documentation](https://hono.dev/docs/api/routing)
[Hono App API](https://hono.dev/docs/api/hono)