Skip to main content

What is Routing in Backend? How Requests Find Their Way Home?

Note: These are notes from Routing lecture/video by Sriniously : https://youtu.be/SubuU1iOC2s?si=Rq3nCiC4bbwIVXsI

1. What is Routing?​

  • The "What" vs. The "Where": In a backend system, HTTP methods (like GET, POST, DELETE) express the what or the intent of a request (e.g., fetching or adding data). Routing expresses the where—the specific resource or destination you want to apply that action to.
  • Definition: Routing is the process of mapping a combination of an HTTP method and a URL path to a specific server-side Handler (a set of instructions or business logic).
  • Uniqueness: The server concatenates the HTTP method and the route to form a unique key. For example, a GET request to /api/books and a POST request to /api/books will trigger completely different logic in the server without clashing.

2. Types of Routes​

There are two primary ways to structure a route path:

  • Static Routes: These are constant strings that do not contain any variable parameters. For example, /api/books will always stay consistent and point to the same general resource.
  • Dynamic Routes: These include variable slots within the URL that the server can extract as data. In most backend frameworks (like Node.js, Python, or Go), these are denoted by a colon, such as /api/users/:id. If a client requests /api/users/123, the server extracts "123" as the ID to fetch that specific user's data.

3. Path Parameters vs. Query Parameters​

When sending data through a URL, backend engineers use two distinct types of parameters:

  • Path Parameters (Route Parameters): These are the variables placed directly inside the route's path, right after a forward slash / (e.g., the 123 in /api/users/123). They are used to express semantic meaning, specifically identifying a unique resource.
  • Query Parameters: Because GET requests do not have a data body, query parameters are used to send key-value pairs of metadata to the server.
    • Syntax: They are attached to the end of the route after a question mark ? (e.g., /api/search?query=some+value).
    • Use Cases: They are heavily used for pagination (e.g., page=2&limit=20), filtering user-defined values, or determining sorting orders (ascending/descending).

4. Nested Routing​

Nested routing is a standard REST API practice used to express a hierarchy between different resources.

  • Semantic Hierarchy: By nesting paths, you create a highly readable, semantic expression of what data you want.
  • Example Workflow:
    • /api/users: Fetches a list of all users.
    • /api/users/123: Goes one level deep to fetch a specific user.
    • /api/users/123/posts: Goes another level deep to fetch all posts created by user 123.
    • /api/users/123/posts/456: Fetches one highly specific post (ID 456) belonging to that specific user.

5. Route Versioning and Deprecation​

As applications grow, business requirements change, which might require you to completely alter the format of the data your API returns (e.g., switching the key name to title).

  • The Problem: If you change the response format on a live route, you will break the frontend application (like an iOS or React app) currently relying on it.
  • The Solution (Versioning): Engineers add version numbers to routes, such as /api/v1/products and /api/v2/products.
  • Deprecation: This allows the server to simultaneously support both the old and new data structures. It provides frontend engineers a safe window of time to migrate their code to v2 before the backend team officially deprecates and removes v1.

6. Catch-All Routes​

  • Purpose: A catch-all route acts as a safety net for invalid requests.
  • How it Works: It is placed at the very end of the server's routing logic, often using a wildcard syntax like /*. If a request trickles down through all the previous route matching algorithms without finding a match, it hits the catch-all.
  • Benefit: Instead of the server defaulting to a broken or null response, the catch-all handler cleanly returns a user-friendly "Route Not Found" (404) message to the client.