Search code examples
angularjsjson-server

Configure redirect of all $http & $resource calls to other endpoint


I would like to run a mock server e.g. JSON Server, and switch between my real Java REST API backend and this JSON Server during development (for instance when real backend services are down). Is there a way to easily configure angular to use a different host for all request?

My setup:

  • localhost:8080 - Spring MVC application, serves SPA + API
  • localhost:8080/api - Spring MVC Rest API
  • localhost:9090/api - JSON Server

And I would like to switch between calls to localhost:8080/api and localhost:9090/api with one singe line for whole Angular application.


Solution

  • @PetrAveryanov gave me hint. Using json-proxy this is as easy as launching proxy:

    json-proxy -p 8081 -f "/api=http://localhost:9090" -f "/=http://localhost:8080"

    And accessing application via port 8081. "Regular" requests will be still passed through to 8080 (Tomcat serving Spring MVC application). All API calls will go to 9090 (JSON Server).