Is there a way to adjust routing based on headers in Elysia?

420 Views Asked by At

I'm attempting to change my routing based on header values. For example, if an incoming request contains an accept header of application/json, I want to call one set of routes to return JSON data, but if it does not have that header, I want to fall back to serving static files.

According to the docs, both the Request and Parse steps come before the routing, and since there isn't an onRouting-type method, my assumption would be to get the headers in either Request or Parse, and adjust something that will affect the routing.

I thought about perhaps using a custom method, so for example if the accept header was application/json, then I could change the method to, say, JSON and then define the rest of my routes like .route('JSON', () => doTheThing()). But of course the request's method is read-only, so I can't really do that.

0

There are 0 best solutions below