- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Comments
For nested resources to work, you need to structure your {
"environments": [
{ "id": 12 },
{ "id": 19 }
],
"reports": [
{ "id": 17, "environmentId": 12 }
{ "id": 25, "environmentId": 12 }
]
} Then |
Ah. kk. Thanks for the update. I'll try that and close this if it works. |
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. |
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)
One more question: should |
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. |
You're right, one level of nesting is supported. 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 However a workaround could be to use filters, there's no limit to how much you can use:
There's also an (old) article but still interesting about deep nesting: 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 :) |
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. |
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. |
Ok, so here's what I got: https://github.com/therebelrobot/json-server/tree/feature/nested-calls I rebuilt 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 :) |
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. |
@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 :) |
The |
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. 😞 |
+1 |
+1 for this feature, @therebelrobot's fork is two years old and I would like much more to see it done here |
@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.
|
@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! |
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 :) |
+1 |
Love to see nested routes too. Even if it is going down 2 levels instead of 1, that would already be nice. |
This would be nice to have |
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. |
So I have a json file that has the following schema:
/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:
The text was updated successfully, but these errors were encountered: