Backend Development › Backend Basics
Routing
Mapping URLs and methods to handler code.
Also known as: routing, routes, URL routing, request routing, route handlers
Routing is how a backend decides which code handles a request: it matches the HTTP method and URL path to a handler function.
# Flask
@app.get("/orders")
def list_orders(): ...
@app.post("/orders")
def create_order(): ...
@app.get("/orders/<int:order_id>") # path parameter
def get_order(order_id): ...
// Express
app.get("/orders/:id", (req, res) => {
res.json(findOrder(req.params.id));
});
When GET /orders/917 arrives, the router finds the matching route, extracts order_id = 917 and calls
the handler (request handler).
What a router does
- Matches method + path pattern, including parameters like
:idor<int:order_id>. - Responds 404 Not Found if no path matches, and 405 Method Not Allowed if the path exists but not for that method.
- Passes request details to the handler: params, query string, headers, body.
- Often lets you group routes under a prefix (
/api/v1) and attach shared behavior (middleware) such as authentication.
Practical tips
- Match order can matter. In some frameworks the first matching route wins, so put specific ones
(
/orders/export) before general ones (/orders/<id>). - Keep handlers thin. Parse input, call a service, return the response. Don’t put business logic in the route (service layer).
- Use consistent, resource-style URLs (endpoints, REST).
- Validate path and query parameters, and convert types (input validation).
- Version public APIs with a prefix or header (API versioning).
Routing is part of the request lifecycle. Every web framework has one, with different syntax and the same job.