Skip to content

Nested requests returning 404 #72

New issue

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

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

Already on GitHub? Sign in to your account

Closed
therebelrobot opened this issue May 5, 2015 · 22 comments
Closed

Nested requests returning 404 #72

therebelrobot opened this issue May 5, 2015 · 22 comments

Comments

@therebelrobot
Copy link

So I have a json file that has the following schema:

{
  "environments": [
    {
      "id": 12,
      "reports": [
        {
          "id": 17
        },
        {
          "id": 25
        }
      ]
    },
    {
      "id": 19,
      "reports": [
        {
          "id": 22
        },
        {
          "id": 30
        }
      ]
    }
  ]
}

/environments works, as does /environments/12, so I figured I could use the route /environments/12/reports to return that array, but it's returning a 404 instead. Am I doing something wrong here?

My env:

  • Mac OS X Yosemite
  • NVM 0.24.0
  • io.js 1.8.1
  • json-server 0.7.3
@typicode
Copy link
Owner

typicode commented May 5, 2015

For nested resources to work, you need to structure your db.json this way:

{
  "environments": [
    { "id": 12 },
    { "id": 19 }
  ],
  "reports": [
    { "id": 17, "environmentId": 12 }
    { "id": 25, "environmentId": 12 }
  ]
}

Then /environments/12/reports should return reports that have environmentId === 12

@therebelrobot
Copy link
Author

Ah. kk. Thanks for the update. I'll try that and close this if it works.

@therebelrobot
Copy link
Author

Worked like a charm. Would you be averse to a PR clarifying nested requests in README.md? I can have it submitted in a few minutes if you want it.

therebelrobot added a commit to therebelbeta/json-server that referenced this issue May 5, 2015
Added a sub-section in the Requests portion to clarify the usage of nested calls a bit. You did list `posts/1/comments` in the list of routes, but it's kinda left up to the reader to figure out what that means. Hopefully this makes it a bit easier to pick up quickly. References [typicode#72](typicode#72)
@therebelrobot
Copy link
Author

One more question: should /environments/12/reports/17 work? I thought it was earlier today, but now I'm running it and no dice. 404 error.

@therebelrobot
Copy link
Author

Hrmmm... looks like not. The nested routes are being handled statically. I'll take a look at it tonight and see if I can't add nested routing ad infinitum.

@typicode
Copy link
Owner

typicode commented May 6, 2015

You're right, one level of nesting is supported.
Except if there's many people that need infinite nesting, it's not wished for the moment.

Mainly to limit code complexity and also based on the idea that if you know the report id, you can directly make a request to get it /reports/17

However a workaround could be to use filters, there's no limit to how much you can use:

/reports?id=17&environmentId=12&...

There's also an (old) article but still interesting about deep nesting:
http://weblog.jamisbuck.org/2007/2/5/nesting-resources

Do you need deep nesting because of a particular front-end library?

PS: thanks for the PR, I'll take a look at it as soon as I can :)

@therebelrobot
Copy link
Author

Thanks for the resource! I'm definitely gonna be going over that.

That being said, the API I'm mocking isn't in my control, I'm building a front-end for it, and I'm just trying to mimic it as closely as possible. It's not that difficult of an update, but if that feature isn't needed in the library as a whole, I'll just source my fork with the changes needed, no worries.

Let me know if there needs to be any changes in that PR, I'm happy to jump in and fix stuff.

@typicode
Copy link
Owner

typicode commented May 6, 2015

Great.

Not sure if it's the best way to fill the gaps for your API, but using the project as a module you could do something like this:

var jsonServer = require('json-server')

var server = jsonServer.create()
server.use(jsonServer.defaults)

// Simple hack
server.use('/environments/:environmentId/reports/:reportId', function (req, res) {
  res.redirect('/reports/' + req.params.reportId)
})

server.use(jsonServer.router('db.json'))

server.listen(3000)

I've not tested code, but it should work.

@therebelrobot
Copy link
Author

Ok, so here's what I got: https://github.com/therebelrobot/json-server/tree/feature/nested-calls

I rebuilt router.js quite a bit, it was hard-coding the routes and duplicating code from show to list. I changed the name of list to parse, and routed all calls through it (it now checks all the ids and manages sub-resources). I split out the show function to find (which handles the actual object finding in lowdb) and present (which handles sorting, paging, etc), making things a little more modular. Also added most internal objects that were in list to req._internal so it could be called by subsequent middleware.

I ran all of it through the test suite, also added 4 more tests for more nested calls. Everything is passing 100%.

Also added a coverage folder, and verified >99% code coverage of the new router.

I know you weren't looking at this kind of feature, but if you want a PR for it after all, lemme know :)

@ir-g
Copy link

ir-g commented May 10, 2015

@typicode @therebelrobot

I would love to see this PR'd in as I really would love to see nested calls working, even if it was just an optional feature.

I wouldn't normally comment, but this feature would be super-useful.

@therebelrobot
Copy link
Author

@isaacrg As per @typicode's desire to keep things simple, I've forked the repo and republished it. Here it is: https://github.com/therebelrobot/json-mock

I'll close this issue now :)

@typicode
Copy link
Owner

The feross/standard is a good point, FWIW the project now follows it. Maybe it can save you time.

@dschu-lab
Copy link

I would also love to see this feature implemented. I'm currently trying to mock an API which is not in my control, and they are using multiple nested resources, even if the id of an element is already known. 😞

@gEndelf
Copy link

gEndelf commented Aug 21, 2016

+1

@patotoma
Copy link

patotoma commented Dec 15, 2016

+1 for this feature, @therebelrobot's fork is two years old and I would like much more to see it done here

@typicode
Copy link
Owner

typicode commented Dec 15, 2016

@patotoma have you tried custom routes https://github.com/typicode/json-server#add-custom-routes? You can map many different kind of routes and for example nested resources.

"/some/nested/route/posts/:postId/comments/:commentId": "/comments/:commentId"

/some/nested/route/posts/1/comments/2 will rewrite to /comments/2

@patotoma
Copy link

@typicode hey man thanks for quick response. Have to say that after a while of using json-server I found out why you made it like this. It wants kind of different thinking that I was used to but now I find using nested routing as bad practice. Route rewrites can be used to map existing apis but from now on I am going to use top level routing only. My team is currently working on a service that will generate db.json for you, so that could be cool. BTW: thanks for your great work!

@ennisa-ire
Copy link

Hey it would be a nice feature to have a deep nested resource feature, the routes approach doesnt cut it. Are there plans to implement it? This would bring already a great service to the next level :)

@lbadi
Copy link

lbadi commented Jan 3, 2018

+1

@jderijke
Copy link

Love to see nested routes too. Even if it is going down 2 levels instead of 1, that would already be nice.

@ghaschel
Copy link

ghaschel commented Jan 7, 2019

This would be nice to have

@Legitcoder
Copy link

Legitcoder commented Jan 10, 2019

Documentation should state there's only one level of nesting. Was trying to figure out why I can't go two levels down, found out through here. Would've been searching and hacking at json-server for a while if I hadn't seen this. You can use ?id=3, but it only works for get. Doesn't work for delete. Still json-server is great since you don't have to wait on a back-end to be fully complete to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests