Routing
Introduction
Routing is the combination of mapping HTTP method (GET, POST, PUT, DELETE) with the URL path to a specific server side handler.
NOTE: Every combination must be and should be unique
Let say:
1. HTTP method GET with route /api/books
2. HTTP method POST with route /api/books
Both the routes are completely unique they perform separate business logic.
Types of Routes:
1. Static Routes: Static Routes are those routes which does not contain any variable parameters. For example /api/books
2. Dynamic Routes: Dynamic Routes are those routes which contains the variable parameters which the server can extract and use it to fetch the data. For example /api/books/123. In this scenario server extracts 123 and treat it as in the form of ID and perform the specified operations.
Path Parameters VS Query Parameters
1. Path parameters: Path parameters are used to express semantic meaning (uniqueness of a resource) of a route. These are the variables that are placed right after the forward slash ‘/’. For example /api/books/123, here 123 represents the path parameters.
2. Query Parameters: Query Parameters are used to send the data in GET route. Since GET route do not have the request body and if we are require to send the data we use query parameters, they send the data in the form of key-value pairs.
- Syntax: /api/books?type=science+friction, query parameters are generally attached after ‘?’ (question-mark)
- Use cases: They are heavily used for pagination
Nested Routing
Nesting routing is the process of fetching the particular data instead of fetching the general data
For example
1. /api/books -> fetches all the books
2. /api/books/authorname -> fetches the books which is written by particular author
3. /api/books/authorname/genre -> fetches the books which is written by particular author and of particular genre
Get Chinmayjain’s stories in your inbox
Join Medium for free to get updates from this writer.
NOTE: Nesting routes are not preferred for perform filtering of the data instead query parameters are recommended to perform filtering of the data. Reason is resource identity vs resource selection
Resource Identity: What I am requesting (Nesting routes)
Resource Selection: How this resource collection be returned
Route Versioning and Deprecation
Since as the application grows business demand changes. We might need to change the response of the api which is quite often in industry. It could be possible that we are required to alter the format of the data in that scenario Route versioning and deprecation plays an important role.
Problem: If we directly changes the response format in the backend and push the changes in the server and note that the application is live then it will break the application. Reason behind is frontend application is still relying on the previous route, you might argue that frontend engineers should update there code simultaneously but still testing time is required, testing on the local environment vs in production grade is very different
Solution (Route Versioning): While creating the apis backend engineers commonly used versions to explicitly show the current version of the particular api. For example: instead of using /api/books use /api/v1/books which denotes version 1 of the api. When we are required to update the api and want to introduce to the new version then /api/v2/books the versioning is very helpful.
Deprecation: In order to deprecate the older version /api/v1/books when backend engineers introduce the v2 of apis then frontend engineers still get the time to migrate there code from v1 to v2. Till the time the code is migrating both the versions are live and supported by backend team. Once all the things are stable and the new api version is checked the older version is officially deprecated.
Catch All Routes
Catch-All routes are the routes which are used to catch all the routes by using the wildcard syntax ‘/*’. It is used so that instead of showing the broken response or null response, server can send the user friendly 404 response to the client that the requested route does not exist.
It is used to catch the invalid requests.







