What is the best way of organizing routes in files? #1717
Replies: 1 comment
-
Two options. It is possible to skip the path parameter and then register route with route. Or provide an empty string for route and register path in the handler file. Here are both examples. It is also possible to have a hybrid path where prefix of the path in in index and the rest in handler. index.tsimport { Hono } from 'hono'
import api1 from './handlers/api1'
import api2 from './handlers/api2'
export default new Hono()
.get('/hello', async (c) => c.text('Hello World!'))
.route('/api1/:id', api1)
.route('', api2) /handlers/api1.tsimport { validator } from 'hono/validator'
import { parse } from 'valibot'
export default new Hono().get( // No need to specify path
validator('param', (value) => parse(api1RequestScheme, value)),
async (c) => {
const { id } = c.req.valid('param')
return c.json({id})
},
) /handlers/api2.tsimport { validator } from 'hono/validator'
import { parse } from 'valibot'
export default new Hono().get('/api2/:id', // Notice that path is provided here and not when registering route
validator('param', (value) => parse(api2RequestScheme, value)),
async (c) => {
const { id } = c.req.valid('param')
return c.json({id})
},
) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
It started with one file where all routes are declared. However it quickly become very long. So I wonder what is the best way to spliting routes into different files? While also maintain types.
I am aware of grouping. However I wonder how can I split routes routes even more. One per file.
Beta Was this translation helpful? Give feedback.
All reactions