Json-server: Custom routes filtering not working

Created on 4 Jul 2017  路  3Comments  路  Source: typicode/json-server

Hi everyone!
I'm having some trouble with access to nested resources.

I'd like to query api/users/:user_id/searches in order to query all the searches of a user. When I do GET searches?user_id=123 everything is fine, but when I do GET api/users/123/searches json-server's response is not filtered by user_id and also search with different user_id are shown. What am I missing?

Thank you in advance,
Manuel

_routes.json_

{
  "/api/": "/",
  "/api/users/:user_id/searches": "/searches?user_id=:user_id"
}

_db.json_

{
    "searches": [
        {
            "id": 1,
            "user_id": 123,
            "type": "by-destination",
            "params": {}
        },
        {
            "id": 2,
            "user_id": 123,
            "type": "by-destination",
            "params": {}
        },
        {
            "id": 3,
            "user_id": 121,
            "type": "by-destination",
            "params": {}
        }
    ]
}

Most helpful comment

The same thing happened to me when I updated to the latest version of json-server. You now need to have the following as the route for /api:

{
  "/api/*": "/$1"
}

The README.md file shows examples of the new routing format, but I couldn't find any mention of the breaking change. It would be great if such a note was added to the README.md file.

All 3 comments

The same thing happened to me when I updated to the latest version of json-server. You now need to have the following as the route for /api:

{
  "/api/*": "/$1"
}

The README.md file shows examples of the new routing format, but I couldn't find any mention of the breaking change. It would be great if such a note was added to the README.md file.

Hi @RobertBernstein,

Thanks for the help.

You can find them in the CHANGELOG file and I bump the minor version when there's a breaking change :)
https://github.com/typicode/json-server/blob/master/CHANGELOG.md#0110---2017-07-05

Thank you, @typicode! I just missed seeing that. I'll keep that in mind for the future.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AdamCook44 picture AdamCook44  路  3Comments

casvil picture casvil  路  4Comments

pantchox picture pantchox  路  3Comments

sboudouk picture sboudouk  路  3Comments

ashleydavis picture ashleydavis  路  3Comments