Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Is it possible to handle nested routes in feather? #97

Closed
tuna123 opened this issue Nov 6, 2014 · 4 comments
Closed

Is it possible to handle nested routes in feather? #97

tuna123 opened this issue Nov 6, 2014 · 4 comments

Comments

@tuna123
Copy link

tuna123 commented Nov 6, 2014

Hello,
I am wondering whether it is possible to handle nested routes using feather. For example:
GET /user
GET /user/5
GET /user/5/items
GET /user/5/items/6

If yes, can you please provide sample code? Appreciate it!

@daffl
Copy link
Member

daffl commented Nov 6, 2014

There is the feathers-associations plugin. Even though it is not fully finished yet it is already possible to do things like:

// Both associations should only work if there is a /users service registered already
app.use('/users', userService)
  .use('/items', itemService);

// Pass service name in an array
// Calls itemService.findAll({ userId: <userId> })
app.associate('/users/:userId/items', ['items']);

So you can do

http://localhost/users/10/items

Which will retrieve all items with userId set to 10. This will work for any service as long as a params.query = { userId: 10 } in itemService.find(params) returns the correct items (the pre-built MongoDB, Mongoose and Memory services already do that).

This does however not allow something like /user/5/items/6. Technically, if 6 is supposed to be the items id it should be unique anyway and equivalent to /items/6.

You can however easily implement your own middleware to e.g. retrieve the user items by their index for this route like so:

app.get('/users/:userId/items/:itemIndex', function(req, res) {
  var itemService = app.service('items');
  var userId = req.params.userId;
  var itemIndex = req.params.itemIndex;

  itemService.find({ query: {
      userId: userId
    }
  }, function(error, items) {
    res.json(items[itemindex]);
  });
});

@daffl
Copy link
Member

daffl commented Nov 24, 2014

Closing this issue as my previous comment hopefully solves it.

@daffl daffl closed this as completed Nov 24, 2014
@ekryski
Copy link
Contributor

ekryski commented Jan 30, 2015

Ya this works but we might need to look at how we handle deeply nested routes.

daffl pushed a commit that referenced this issue Aug 25, 2018
Promoting the use of feathers-cli
daffl pushed a commit that referenced this issue Aug 29, 2018
daffl pushed a commit that referenced this issue Aug 29, 2018
@lock
Copy link

lock bot commented Feb 8, 2019

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue with a link to this issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 8, 2019
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants