H
Hono7mo ago
Korny

Any way to modify the request path in middleware?

(this is cross-posted on https://github.com/orgs/honojs/discussions/2642 - I'm not quite sure which forum we are meant to use? trying both, apologies for the noise) I am trying to replicate a legacy API which had optional versions in paths, so GET /v1/foo actually called GET /foo under the covers. I was hoping to add some middleware that just intercepted paths starting with /v1 and removed the prefix (or re-directed somehow to the correct route) - but it seems that modifying the ctx.req.path does nothing. Is this possible? I've seen examples that clone ctx.req.raw and make a brand new request - that feels a bit ugly, and I'm not sure that Hono will use the new request's path for routing anyway. Any suggestions? (for added complexity we are using the zod-openapi middleware so I can't just give our routes both /foo and /v1/foo paths easily. And we'd like to decrement the /v1 rubbish anyway, so I'd prefer to not have it in our openapi schema)
2 Replies
Nico
Nico7mo ago
What are you using to run the application using redirects on your server sounds like the best option To calify I am not talking about runtime, rather server like nginx or an automated service like DO apps
Korny
KornyOP7mo ago
yeah, I could add a redirect inside our k8s cluster (we already have some) For now though I just moved to on('GET', ['/foo','/v1/foo'],... - had to upgrade hono to get there! I also killed off the zod-openapi middleware, so I can hide endpoints like /v1/foo from our API docs.
Want results from more Discord servers?
Add your server