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.

5
  • stackoverflow.com/questions/17876439/… Commented Feb 21, 2017 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. Commented Feb 21, 2017 at 15:28
  • Please check updated question Commented Feb 21, 2017 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'). Commented Feb 21, 2017 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? Commented Feb 21, 2017 at 16:08

2 Answers 2

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'); 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.