Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json-server - Using nested request

I just start using json-server and struggling with one thing. I want to have URL which are nested so e.g. to get user orgs, request would looks like: /rest/user/orgs and will return array of user orgs

{
    "rest": {
        "user": {
            "select": {
                "org": []
            },
            "orgs": [{
                "id": "5601e1c0-317c-4af8-9731-a1863f677e85",
                "name": "DummyOrg"
            }],
            "logout": {}
        }
    }
}

Any idea what I am doing wrong?

like image 251
Andurit Avatar asked Apr 11 '17 15:04

Andurit


1 Answers

This is not supported by the library. The way to get this working is to add a custom routes file to de server, where you will map (or redirect) requests made to /rest/user/ to /.

db.json

 {
            "select": {
                "org": []
            },
            "orgs": [{
                "id": "5601e1c0-317c-4af8-9731-a1863f677e85",
                "name": "DummyOrg"
            }],
            "logout": {}
    }

routes.json

{
  "/rest/user/*": "/$1"
}

and then run it using json-server db.json --routes routes.json

like image 122
Nicolas Avatar answered Oct 21 '22 19:10

Nicolas