3 ms·
> Routers are easy; you likely don't need more than a few lines of code to accomplish most of what you need. Router as in delegating a URL to a handler for tha
by deathanatos 11y ago
> Routers are easy; you likely don't need more than a few lines of code to accomplish most of what you need.
Router as in delegating a URL to a handler for that URL? I wrote one once … and it is deceptive and not easy; most of the server side ones that I've encountered (e.g., Django, Flask, nginx) do a poor job.¹
The naive implementation of a mapping of regexes to handlers is dead simple to implement, but not very ergonomic to really use. Something that understands that paths are hierarchies works a lot better. Then it's also nice to have something that can stub out a component to a variable (and preferably type check that), e.g., "/customers/:customer_id", and pass you "customer_id" after validating it to be an integer, a UUID, or whatever. Then you need the capability to delegate an entire subtrees to a subrouter, and maybe if we could just get some handling of delegation based on the method in here, but HEAD needs to be automatic…, and canonicalization of URLs would be nice too.
¹I feel like this is one of those things that until you've seen something better, you're still wanting the faster horse.