json-server icon indicating copy to clipboard operation
json-server copied to clipboard

Can not support route more than 3 levels?

Open littlee opened this issue 10 years ago • 6 comments

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 avatar Nov 08 '16 02:11 littlee

@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())
}) 

limsim avatar Nov 12 '16 09:11 limsim

@littlee

Assumptions

  • You are using json-server with 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.

Nilegfx avatar Nov 12 '16 13:11 Nilegfx

@typicode please confirm.

I confirm :) and @Nilegfx thank you for the really great explanation and help with issues :+1:

typicode avatar Nov 22 '16 12:11 typicode

@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 avatar Jan 31 '17 19:01 dhruvb14

@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" } }

kaustubhtalathi avatar Oct 31 '17 16:10 kaustubhtalathi

@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

riyazmulla51 avatar Apr 20 '20 09:04 riyazmulla51