Hono

Hono is a lightweight, ultrafast web framework for building APIs, especially suited for serverless and edge environments.

Project Structure

hono-project/
├── src/
│   ├── index.ts
│   ├── routes/
│   │   ├── index.ts
│   │   └── user.ts
│   ├── middlewares/
│   │   └── auth.ts
│   └── utils/
│       └── logger.ts
├── test/
│   └── main.test.ts
├── .env
├── package.json
├── tsconfig.json

src/index.ts

Entry point of the application. Here we create the app and register the routes and middleware.

import { Hono } from 'hono'
import { logger } from './utils/logger'
import { authMiddleware } from './middlewares/auth'
import userRoutes from './routes/user'

const app = new Hono()

app.use('*', logger)
app.use('/user/*', authMiddleware)

app.route('/user', userRoutes)

app.get('/', (c) => c.text('Hello from Hono!'))

export default app

routes/user.ts

Defines user-related endpoints in a modular fashion.

import { Hono } from 'hono'

const user = new Hono()

user.get('/', (c) => c.json({ message: 'Get all users' }))
user.get('/:id', (c) => {
  const id = c.req.param('id')
  return c.json({ id })
})

export default user

middlewares/auth.ts

Custom middleware for authentication.

import type { MiddlewareHandler } from 'hono'

export const authMiddleware: MiddlewareHandler = async (c, next) => {
  const token = c.req.header('Authorization')
  if (!token || token !== 'Bearer your_token_here') {
    return c.json({ error: 'Unauthorized' }, 401)
  }
  await next()
}

utils/logger.ts

Simple logging middleware.

import type { MiddlewareHandler } from 'hono'

export const logger: MiddlewareHandler = async (c, next) => {
  console.log(`[${c.req.method}] ${c.req.url}`)
  await next()
}

Testing with Vitest

Unit tests are placed in the test directory. Use vitest for testing.

import { describe, it, expect } from 'vitest'

describe('Sample test', () => {
  it('adds numbers', () => {
    expect(2 + 2).toBe(4)
  })
})

Running Tests

npm run test

Environment Variables

Use .env file to manage sensitive configs like API keys or tokens.