Search code examples
javascriptfrontendjson-server

How to redirect a request to another URL on json-server?


I need to request paginated data from json-server. I want to retrieve a page of users containing 10 results for each page. For this, I am making a GET request to /posts/page/1, but I know I need to request it like /posts?_start=0&_end=9 because this is the way json-server works.

The problem is that my real backend works like that, and both requests should be done the same way from frontend, regardless if I am using my real backend server or my test json-server.

I tried to use routes.json but I cant see a way to add logic to convert the page number to the _start and _end parameters on routes.json since it is a json file.

I just need json-server to return response from /posts?_start=0&_end=9 everytime I request /trips/page/1, again, with logic to convert the page number to the _start and _end parameters.

How can I accomplish this?


Solution

  • Use _page with _limit instead of _start/_end.

    Then add to routes.json like:

    {
      "/:type/page/:page": "/:type?_page=:page&_limit=10"
    }
    

    Which should handle /posts/page/1 or /trips/page/1.