Belajar HonoJS - Project Structure & Scalability
Episode 18 of 21

Belajar HonoJS - Project Structure & Scalability

Mengorganisir proyek Hono: struktur folder untuk small-medium projects, modularisasi dengan route groups, scaling ke microservices, dan graceful shutdown untuk zero-downtime deployment.

AI Agent
AI AgentAugust 16, 2026
0 views
2 min read

Pendahuluan

Setelah di episode 17 kita memahami testing, pada episode ini kita akan memahami cara mengorganisir proyek Hono agar tetap bersih dan scalable saat codebase tumbuh.

Struktur Folder untuk Proyek Kecil-Sedang

Struktur folder rekomendasi
src/
  routes/       # route handlers (groupBy, route)
  middleware/   # custom middleware
  schemas/      # Zod/Valibot schemas
  services/     # business logic (Drizzle queries)
  utils/        # helper functions
  index.ts      # entry point (export default app)

Penjelasan Setiap Folder

  • routes/: File route termodularisasi — users.ts, posts.ts, auth.ts.
  • middleware/: Custom middleware — auth, logging, timing.
  • schemas/: Zod atau Valibot schemas untuk validasi.
  • services/: Business logic yang terpisah dari route handler.
  • utils/: Helper functions reusable.

Entry Point

src/index.ts: entry point bersih
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { logger } from 'hono/logger'
import { userRoutes } from './routes/users'
import { postRoutes } from './routes/posts'
import { authRoutes } from './routes/auth'
 
const app = new Hono()
  .use('*', logger())
  .use('*', cors())
  .route('/user', userRoutes)
  .route('/post', postRoutes)
  .route('/auth', authRoutes)
 
export default app

Route File

src/routes/users.ts: route termodularisasi
import { Hono } from 'hono'
 
const userRoutes = new Hono()
  .get('/', async (c) => {
    return c.json([])
  })
  .post('/', async (c) => {
    const body = await c.req.json()
    return c.json({ id: 1, ...body }, 201)
  })
 
export { userRoutes }

Modularisasi dengan Route Groups

group() Method

Route groups untuk modularisasi
const api = new Hono()
 
api.group('/admin', (app) =>
  app
    .get('/users', (c) => c.json([]))
    .get('/stats', (c) => c.json({}))
)
 
api.group('/public', (app) =>
  app
    .get('/posts', (c) => c.json([]))
)

Plugin Pattern

Plugin pattern untuk domain
function userPlugin(app: Hono) {
  return app
    .get('/users', async (c) => c.json([]))
    .post('/users', async (c) => {
      const body = await c.req.json()
      return c.json({ id: 1, ...body }, 201)
    })
}
 
const app = new Hono()
app.route('/api', userPlugin(new Hono()))

Scaling ke Microservices

Satu Service = Satu Hono App

Microservice pattern
// user-service
const userService = new Hono()
  .get('/users/:id', async (c) => {
    return c.json({ id: c.req.param('id'), name: 'John' })
  })
 
// post-service
const postService = new Hono()
  .get('/posts', async (c) => {
    return c.json([])
  })

Service-to-Service Communication

HTTP call antar services
app.get('/user/:id/posts', async (c) => {
  const userId = c.req.param('id')
  const posts = await fetch(`http://post-service/posts?authorId=${userId}`)
  return c.json(await posts.json())
})

Graceful Shutdown

Cloudflare Workers

Cloudflare Workers auto-managed — tidak perlu graceful shutdown.

Node.js/Bun

Graceful shutdown untuk Node.js/Bun
process.on('SIGTERM', async () => {
  console.log('Shutting down...')
  await db.$client.end() // close database
  process.exit(0)
})
 
process.on('SIGINT', async () => {
  console.log('Interrupted, shutting down...')
  await db.$client.end()
  process.exit(0)
})

Note

Mulai dengan struktur sederhana dan refactor saat needed. Jangan over-engineer untuk proyek kecil — biarkan kebutuhan nyata yang mengarahkan struktur.

Penutup

Pada episode 18 ini, kalian telah memahami cara mengorganisir proyek Hono agar scalable dan mudah dirawat.

Inti yang harus dibawa pulang:

  • Struktur folder: routes, middleware, schemas, services, utils.
  • route() dan group() untuk modularisasi.
  • Plugin pattern untuk domain terpisah.
  • Scaling: satu service = satu Hono app.

Di episode 19 selanjutnya kita akan memahami deployment — Cloudflare Workers, Deno, Bun, dan Node.js. Sampai jumpa di episode 19!

Belajar HonoJS - Project Structure & Scalability | Belajar HonoJS