22 Replies
use-case, recursive add routes
uhhhh, can you explain in words what your goal is?
btw,
c.req.path
is not determined by your routes, it is taken from the actual Request.url
using getPath
, and you can provide an override to that function by passing it to the top-level Hono
instance (doesn't work on sub-instances) https://hono.dev/docs/api/routing#routing-with-host-header-valuethe goal is to use
_basePath
as route path,.
yea, just noticed itthat is already done by you routing on
/
the sub-routes
and you said you didn't want that
so I am asking what do you want, if not that?yeah, when i compare the
routePath
give a full code example
I understand nothing from that
are you reading the file system on runtime to dynamically register routes?
yes
do you dislike re-exporting your routes in an index file?
yea, cu'z it's hassle to manually add it, and it will make the code long
it being a hassle I can understand, but how can it make the code long?
let me give you an example
>
src/controllers/Users.ts
> src/controllers/Posts.ts
> src/controllers/index.ts
> src/main.ts
the code doesn't get "long", for each new file, you simply add a new line to the src/controllers/index.ts
file
a minor hassle indeed
but how often do you add new controllers?
---
if your use case requires dynamic route registration then I concedethis looks messy for me.. because i don't do controllers
if the name bothers you, then don't name it that way
nothing changes tho
yes, but that's the way of controllers
it's just a way of structuring your sub-routes, you are already doing smth similar by reading a folder and dynamically adding each file as a sub-route
anyways,. thanks for your time ❤️ gtg
unless I misunderstood you
in which case I apologize for asserting my opinion like that
and would like to continue helping you with other alternatives
hmm, you mentioned recursive sub-routes, which means you have file-based routing as your goal?
I understand what you meant by it not being a controller now
yes, when the
app
default
is instanceof Hono
then it will register as routeI see, similar to what HonoX is doing, but w/o any jsx?
hmm, yea
man, I got excited about this, I don't really need HonoX, but having an alternative for file-based routing in the form on a vite plugin sounds awesome.
I'll go ahead and make such a plugin a reality myself :)
I made this template
https://github.com/ArjixWasTaken/vite-hono-file-based-routing
doesn't play well with
basePath
, but hopefully you won't need it :)
and hey, you can modify the virtual module to work the way you want, it's not smth published on npm, so you can modify it!
you could remove ts-morph, I initially added it bc I planned to check the default export of each file to determine if it's a route or not, but honestly, you should not have random files lying alongside your routes in the first place, so I skipped that