1

Some website's APIs let you do something like this:

            Initial user's id          their first friend, also a user
                 v                     v
GET /api/users/54282/friends/0/friends/0
                             ^
                        first item in array, would also be a user

Rather than

            Initial user's id
                 v
GET /api/users/54282/friends
Look at friends[0].id to get the first friend's id

            First friend's id
                 v
GET /api/users/97420/friends
Look at friends[0].id to get the first friend's id

            First friend's id
                 v
GET /api/users/31230

Another example although i'm not sure if Github allows this

Get the 10th collaborator's repos
GET /api/users/sharedrory/repos/312388/collaborators/10/repos

How do would you implement something like this in express?

SharedRory
  • 246
  • 6
  • 17
  • The inefficient way to implement it is to query each time you get a request and return just the index of the requested item. The efficient way would involve some sort of query cache so subsequent requests don't have to rerun the query every time. – jfriend00 Sep 28 '21 at 20:02
  • Or, some databases will have a way to query for a specific index range and leave the caching to the database itself. – jfriend00 Sep 28 '21 at 20:11
  • More typical in API design is an ability for a client to request a range of values and receive a chunk of results back. You never want a client making separate requests for the 0th item, then the 1st item, then the 2nd item and so on as that's very inefficient use of the server. – jfriend00 Sep 28 '21 at 20:13
  • 1
    @jfriend00 If I wanted all the item in the array, I wouldn't append /. Instead, I want say friends of a user's first friend. – SharedRory Sep 28 '21 at 20:35

1 Answers1

1

It seems the term I wanted was "nested"/"nesting"

Rest with Express.js nested router https://gist.github.com/zcaceres/f38b208a492e4dcd45f487638eff716c

SharedRory
  • 246
  • 6
  • 17