Hono: Middleware

A middleware is a function that “hooks” into the routing process, performing any kind of operation.

You can hook into a Request before it’s processed, or a Response before it’s sent.

We have a lot of built-in middleware, and you can plug your own, or reuse any middleware released by the community.

I won’t go into creating your own, but it’s nice to see a couple examples of what middleware can do.


Simplest example: a logger.

import { logger } from 'hono/logger'


app.use('*', logger())

This will print a log of all the server activity to the console:

Basic authentication

You can password protect a set of routes:

import { basicAuth } from 'hono/basic-auth'

app.use('/dashboard', basicAuth({ 
  username: 'test', 
  password: 'test'

app.get('/dashboard', (c) => { 
  return c.text('Logged in') 

You’ll see the page only if you add the correct credentials, otherwise you’ll see a “401 Unauthorized” message.

Implement CORS

import { cors } from 'hono/cors'  


app.use('/api/*', cors())

(you can use tons of options)

Set cache headers

import { cache } from 'hono/cache'


    cacheName: 'my-app',
    cacheControl: 'max-age=3600',

Set compression headers

import { compress } from 'hono/compress'


app.use('*', compress())

… there’s a lot more, and you should check the official documentation.

Lessons this unit:

0: Introduction
1: Your first Hono app
2: The Request object
3: Send a response to the client
4: Manage cookies
5: Work with HTTP headers
6: Handling redirects
7: Routing
8: JSX templates
9: ▶︎ Middleware
10: Hono on Node.js
11: Handling forms COMING SOON