Can not support route more than 3 levels?
db.json
{
"p/p/p": [
{
"id": 1,
"name": "my_name",
"value": "my_value"
}
]
}
will get 404 when accessing /p/p/p
WHILE
{
"p/p": [
{
"id": 1,
"name": "my_name",
"value": "my_value"
}
]
}
works...
@littlee my guess is that when you have a 3rd level json-server thinks the 3rd level is an ID. It seems to be the pattern that it's following. I'm facing the same problem. My solution is to write a simple custom route.
Something like that
server.get('/p/p/p', function(req, res) {
res.jsonp(db.get('p/p/p').value())
})
@littlee
Assumptions
- You are using
json-serverwith version <0.9.0 - This issue was the motive for the original author to avoid any confusion by adding a validation layer here for whatever go to the "in-memory" database shouldn't contain keys that has slashes in version 0.9.0.
To confirm your json-server version, run json-server -v if you installed it globally, or node_modules/.bin/json-server -v from your project's root directory if you installed it as dev or production dependency
Explanation
The problem is what you did "and looks for you like a working routing in json-server<0.9.0" is not routes/routing. it is just a simple key/value assigning.
I think the confusion comes from using slashes / for naming a resource/key.
When you do p/p this is just a normal json key not the path/route in which the request should take.
Given that, if you did a request to /p/p it works because indeed there IS a key called p/p in your original db.json file
to prove that, change it to something else like p*p and did a request to /p*p it will work.
Solution If you want to have custom route and don't want to use json-server as module then write custom route / rewrite, you could do it simply by creating two files
db.json
{
"p": [{
"id": 1,
"name": "my_name",
"value": "my_value"
}]
}
customRouter.json
{
"/p/p/p": "/p"
}
Then from your terminal, run:
json-server db.json --routes customRouter.json
now both requests are equal
/p/1
/p/p/p/1
so if you upgraded your json-server version now you will not able to run it and you will get error message looks like this:
Oops, found / character in database property 'p/p'.
/ aren't supported, if you want to tweak default routes, see
https://github.com/typicode/json-server/tree/next#add-custom-routes
@limsim
Your guess is "somehow" correct.
ِAthough your approach is "technically" correct, it requires setting up json-server as module and if you did so, then you should use the custom routes (explained above), it's much easier and cleaner. middlewares are for adding logic or (very complex/dynamic routing) not simple routing which could be done by another feature/options of the server.
I hope this helps. if you want more clarification, please post your implementation here and let's discuss it further, happy to help.
@typicode please confirm.
@typicode please confirm.
I confirm :) and @Nilegfx thank you for the really great explanation and help with issues :+1:
@Nilegfx your description was very helpful to me also. I have another issue though. So I got nesting working to 4 levels like I need but am hitting an issue with what I think is escaping characters.
`server.use(jsonServer.rewriter({
"/_api/web/lists/getbytitle('Site%20Creation')/items": '/SiteCreation',
"/_api/web/lists/blah": '/comments',
}))`
The first one 404's but second one is fine. So I know its not the depth of the path but probably due to the fact of the parenthesis and quotes... Any help?
@dhruvb14 You can try adding a pathRewrite in your proxy.conf.json: ex: "/p/p/a": { "target": "http://localhost:3000", "secure": false, "pathRewrite": { "^/a": "/a" } }
@Nilegfx and @typicode Thanks for the above explanation. For me it is working for the single json (in you case db.json) file. I wanted this solution for multiple json files.
For multiple files i am using the below solution. #45 (comment)
Using this solution for Nested requests For this i replaced 1 of my json file as mentioned in the below comment #72 (comment)
I am getting 404 for Nested requests. Is there anything i am missing here? Thanks in advance