Getting Started with HonoJS for Lightweight Web APIs

Building modern web APIs shouldn’t require heavy frameworks or complex setups. If you’re looking for a fast, lightweight alternative to Express or Koa that works across multiple JavaScript runtimes, HonoJS might be exactly what you need.
Key Takeaways
- HonoJS is an ultrafast web framework that runs on any JavaScript runtime including Node.js, Bun, Deno, and Cloudflare Workers
- Built on Web Standards like Request, Response, and Fetch API for maximum portability
- Minimal bundle size under 20KB with better performance than Express
- Includes essential middleware for CORS, logging, and compression out of the box
What Makes HonoJS Different
HonoJS is a small, ultrafast web framework built on Web Standards. Unlike traditional Node.js frameworks, it runs on any JavaScript runtime—Node.js, Bun, Deno, Cloudflare Workers, AWS Lambda, and more. This portability comes from its foundation on standard Web APIs like Request, Response, and Fetch.
import { Hono } from 'hono'
const app = new Hono()
app.get('/', (c) => c.text('Hello Hono!'))
export default app
That’s a complete HonoJS application. No boilerplate, no complex configuration—just clean, modern JavaScript.
Setting Up Your First HonoJS API
Let’s build a simple REST API to see HonoJS in action. First, create a new project:
npm create hono@latest my-api
Choose your preferred runtime (Node.js, Bun, or Cloudflare Workers). For this example, we’ll use Node.js:
cd my-api
npm install
npm run dev
Your server is now running at http://localhost:3000
.
Building REST Routes with HonoJS
HonoJS uses a familiar routing syntax that feels natural if you’ve used Express:
import { Hono } from 'hono'
const app = new Hono()
// GET endpoint
app.get('/api/users', (c) => {
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
]
return c.json(users)
})
// POST endpoint with JSON parsing
app.post('/api/users', async (c) => {
const body = await c.req.json()
// In production, save to database
return c.json({ message: 'User created', data: body }, 201)
})
// Dynamic routes
app.get('/api/users/:id', (c) => {
const id = c.req.param('id')
return c.json({ id, name: 'User ' + id })
})
export default app
The Context object (c
) provides everything you need: request data, response helpers, and utilities for headers, cookies, and more.
Discover how at OpenReplay.com.
Middleware for Production-Ready APIs
HonoJS includes essential middleware out of the box. Here’s how to add CORS, logging, and compression:
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { compress } from 'hono/compress'
const app = new Hono()
// Apply middleware
app.use('*', logger())
app.use('*', cors())
app.use('*', compress())
// Your routes here
app.get('/api/health', (c) => c.json({ status: 'ok' }))
export default app
Creating custom middleware is straightforward:
// Simple auth middleware
const authMiddleware = async (c, next) => {
const token = c.req.header('Authorization')
if (!token || token !== 'Bearer secret-token') {
return c.json({ error: 'Unauthorized' }, 401)
}
await next()
}
// Apply to specific routes
app.use('/api/protected/*', authMiddleware)
app.get('/api/protected/data', (c) => {
return c.json({ secret: 'This is protected data' })
})
Runtime Portability: Deploy Anywhere
One of HonoJS’s biggest advantages is its runtime flexibility. The same code works across different platforms with minimal changes:
For Cloudflare Workers:
export default app
For Node.js:
import { serve } from '@hono/node-server'
serve({ fetch: app.fetch, port: 3000 })
For Bun:
export default { port: 3000, fetch: app.fetch }
This means you can start development locally and deploy to edge locations without rewriting your API.
Extending with Database Integration
Here’s a quick example using Drizzle ORM with HonoJS:
import { Hono } from 'hono'
import { drizzle } from 'drizzle-orm/better-sqlite3'
import Database from 'better-sqlite3'
import { postsTable } from './schema'
const sqlite = new Database('app.db')
const db = drizzle(sqlite)
const app = new Hono()
app.get('/api/posts', async (c) => {
const posts = await db.select().from(postsTable)
return c.json(posts)
})
app.post('/api/posts', async (c) => {
const { title, content } = await c.req.json()
const result = await db.insert(postsTable).values({ title, content })
return c.json(result, 201)
})
export default app
Performance Benefits
HonoJS APIs consistently outperform Express in benchmarks. Its minimal overhead and Web Standards foundation mean faster response times and lower memory usage. For edge deployments, the small bundle size (under 20KB) translates to quicker cold starts.
Conclusion
HonoJS offers a refreshing approach to building web APIs. Its combination of simplicity, performance, and runtime flexibility makes it an excellent choice for modern JavaScript applications. Whether you’re building microservices, serverless functions, or traditional REST APIs, HonoJS provides the tools you need without the bloat.
Start with a simple API, add middleware as needed, and deploy anywhere—that’s the HonoJS way.
FAQs
HonoJS typically shows 2-3x better performance than Express in benchmarks due to its minimal overhead and Web Standards foundation. It has faster response times, lower memory usage, and significantly quicker cold starts for serverless deployments.
Yes, HonoJS works with most Node.js packages when running on Node.js or Bun. However, for edge runtimes like Cloudflare Workers, you'll need packages that are compatible with Web Standards and don't rely on Node.js-specific APIs.
Absolutely. HonoJS is production-ready and used by many companies for APIs handling millions of requests. Its lightweight nature, built-in TypeScript support, and comprehensive middleware ecosystem make it ideal for both small and large-scale applications.
The learning curve is minimal. HonoJS uses similar routing patterns and middleware concepts as Express. The main differences are the Context object instead of req/res and the use of Web Standards APIs. Most Express developers can be productive with HonoJS within hours.
Gain Debugging Superpowers
Unleash the power of session replay to reproduce bugs, track slowdowns and uncover frustrations in your app. Get complete visibility into your frontend with OpenReplay — the most advanced open-source session replay tool for developers. Check our GitHub repo and join the thousands of developers in our community.