0

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.

dragonfly
  • 17,407
  • 30
  • 110
  • 219
  • http://stackoverflow.com/questions/17876439/configuration-file-in-angularjs – Ankh Feb 21 '17 at 15:24
  • Not sure what you are using to route your requests to the said backend services, but if you are using a node/Express backend, you can set and use environment variables to detect if you are in dev and use that logic to route the requests. – frishi Feb 21 '17 at 15:28
  • Please check updated question – dragonfly Feb 21 '17 at 15:35
  • 1
    you need either to write your code that you can switch (that actually good practice), or you need to proxy requests (we are doing this with webpack -- just google something like 'proxy + json-server'). – Petr Averyanov Feb 21 '17 at 15:51
  • @PetrAveryanov: if I get it correctly, this proxy with webpack can be done if backend runs in node/uses webpack dev server? – dragonfly Feb 21 '17 at 16:08

2 Answers2

0

We can set prefixURI variable to quickly change to pointing different API.

Usage:

var prefixURI ='https://••••':
//var prefixURI = '';//if you are on same port it is empty
$http.get(prefixURI+'posts');
raja reddy
  • 372
  • 2
  • 9
0

@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).

dragonfly
  • 17,407
  • 30
  • 110
  • 219