Your code example, I assume it's Javascript, is as unstructured as possible. The real world is less trivial.
For example, if you need "/routes" to list all routes you MUST have a map of routes to an object or function describing what to answer for that route, so that the code for "/routes" can inspect it and work without duplicating information.
Then populating and passing around this configuration object becomes the core business of your request dispatching framework, along with fixed, centralised behaviours (e.g. testing authentication and answering 403 instead of serving the regular document if not passed) and reusable mechanisms (e.g. extracting HTTP headers and request parameters into nice data structures and making them available to request handlers).
Comments
Your code example, I assume it's Javascript, is as unstructured as possible. The real world is less trivial.
For example, if you need "/routes" to list all routes you MUST have a map of routes to an object or function describing what to answer for that route, so that the code for "/routes" can inspect it and work without duplicating information.
Then populating and passing around this configuration object becomes the core business of your request dispatching framework, along with fixed, centralised behaviours (e.g. testing authentication and answering 403 instead of serving the regular document if not passed) and reusable mechanisms (e.g. extracting HTTP headers and request parameters into nice data structures and making them available to request handlers).
Can't you use a mature existing library?