When creating method routes, do we need to keep an order?

Question

When creating method routes, do we need to keep an order?

Answer

There is a certain specificity on how we arrange our routes based on the route and then on the method. We want to keep the order in which requests can come in, that is:

1st: the root route /

This is most commonly only a GET request to the server in general, if we talk about a web app that has a server on its own like Facebook for example, the root route request will fetch all necessary static pages and data to give us the login or home page (depending on what the browser has already stored).

2nd: specific routes, ie: /expressions/ and /animals/

These are the routes that will directly relate to content in bulk, getting all the animals or expressions and creating a new animal or expression. it is important that for each one of these routes you will follow it by its longer paths, for example, setting up:

GET /expressions/
POST /expressions/
GET /expressions/:id
PUT /expressions/:id
DELETE /expressions/:id

and after, having the following specific route:

GET /animals/
…

which leads us to

3rd: longer routes, ie.: /expressions/:id

this mainly comprises of three functions, Read one object (GET by id), Update (PUT) or Delete (as we guessed DELETE).

That said, there is no greater reason to this than structure since express matches route and method, but it makes it more legible and easier to debug if we ever encounter any issues.

7 Likes

I don’t get at all what you mean by that. Could someone explain or reformulate?

3 Likes

For the 3rd callout on longer routes, do you mean ie. /animals/:id ?
As stated, I don’t understand why GET /animals/ leads us to /expressions/:id, which is just a repeat of the above.

2 Likes

@adrmac @mariaburmeister
The ordering of routes is for ease of debugging mainly. Meaning that if we are having issues with an “expressions” request we know exactly where to look in our code to solve the issue. The order is

  1. the root route/ eg a homepage
  2. specific route e.g “/expressions”
  3. Longer routes of the specific path e.g “/expressions/:id”

Basically, all routes of the same path should be lumped together, that’s a specific route followed by its longer routes, so we could have something like:

So when debugging, If there’s an issue handling the “animals” request, we can simply check the part of the code containing “/animals” routes.

3 Likes