H
Hono3w ago
Chris

Hono for Large Scale Apps

Hello, We have decided to use Hono as our API framework for our new web app. We have used it for a few months now and finding it fantastic. My only confussion is since the start, I had the typical controller, service and repository structure. I recently discovered on the Hono documentation that they do not recommended the typical 'RoR' controller pattern. I completley get the reason, but would like to know of examples of how people are building apps with many routes and how they are splitting up their business & database logic. Thanks
2 Replies
ambergristle
ambergristle3w ago
imo hono is a backend-first framework. while you can use it to build full-stack apps, i think it lends itself best/most directly to lightweight backend services or data apis if an app doesn't need to use RPC, then you don't need to chain methods or worry (much) about abstracting controllers. even if it does need some kind of TS client, many devs opt for something more advanced, be it something like react-query, an auto-gen client, etc. the truth is that you can get pretty far with the recommended structure though, just by pulling out behavior (e.g., db reads/writes)
const app = new Hono()
.get('/users', zValidator('query', QuerySchema), async (c) => {
const { email } = c.req.valid('query');
const users = await findUsersByEmail(c.var.db, email);
return c.json(users);
})
const app = new Hono()
.get('/users', zValidator('query', QuerySchema), async (c) => {
const { email } = c.req.valid('query');
const users = await findUsersByEmail(c.var.db, email);
return c.json(users);
})
altenatively, lots of devs also end up using openapi. both @hono/zod-openapi and hono-openapi close the typing gap that hono leaves. you can essentially use them to type-safely abstract controllers and then there are the factory helpers, which are the recommended alternative to inlining handlers, though imo they're more useful for coupling related middleware/handlers short of using openapi, this is my preferred way of building hono backends
Chris
ChrisOP3w ago
Thanks for this. Was super helpful and pretty much followed this pattern.

Did you find this page helpful?