Home > Blockchain >  NodeJS Express recursive routes
NodeJS Express recursive routes

Time:10-05

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?

CodePudding user response:

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

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

  • Related