-2

Problem Statement: Client/UI developed using angular-cli and Angular-5, UI is running on http://localhost:4200 and we have back-end server running on http://localhost:8000 that means all api's called like http://localhost:8000/users/getuserinfo and returns some json data properly. Also having other routes like http://localhost:8000/student/getresult etc.

So i have to create proxy for this as when i run ng serve my application open-up in browser on http://localhost:4200, but while making call to api's url forms like http://localhost:4200/users/getuserinfo which return result like 404 i.e. not found, but when i hit url in another tab for http://localhost:8000/users/getuserinfo, it returns me json data.

So i need to create proxy for http://localhost:4200/users/getuserinfo to like as http://localhost:8000/users/userinfo. I tried to implement using DOCS but not succeeded and also dont know how get all apis in proxy.conf.json file, can some one help me here?

Below is my code,

{
   "/users/userinfo": {
   "target": "http://localhost:8000",
   "secure": false
   }
}
Vinod
  • 1,234
  • 3
  • 21
  • 37
  • *"not sucedded"* isn't much of a [mcve]. Maybe see https://github.com/angular/angular-cli/wiki/stories-proxy – jonrsharpe Apr 25 '18 at 11:31
  • 1
    Possible duplicate of [angular-cli server - how to proxy API requests to another server?](https://stackoverflow.com/questions/37172928/angular-cli-server-how-to-proxy-api-requests-to-another-server) – mxr7350 Apr 25 '18 at 11:33
  • 1
    See here https://stackoverflow.com/questions/37172928/angular-cli-server-how-to-proxy-api-requests-to-another-server – Shrivats Apr 25 '18 at 11:45
  • 1
    @Vinod Hope you are aware that The proxy configuration is intended to proxy calls when running the dev server via `ng serve`. After you run `ng build` you are responsible for the web server and its configurations – Vikas Apr 25 '18 at 13:45

1 Answers1

1

Define a proxy file for your api and start the server with this command:

ng serve --proxy-config proxy.conf.json

proxy.conf.json

{
  "/users": {
    "target": "http://localhost:8000",
    "secure": false
  }
}

I find that it is easier to prefix all my api routes with /api. This makes it easier when serving the application in production because you can return the app on all routes that don't begin with /api and it makes the proxy config just as simple because you only need to setup one rule as opposed to potentially multiple using your current stategy.

Teddy Sterne
  • 13,774
  • 2
  • 46
  • 51
  • thanks, it helps. Below is my proxy.conf.json file `{ "/users": { "target": "http://localhost:8000", "secure": false }, "/student": { "target": "http://localhost:8000", "secure": false }, ... ... ... }` – Vinod Apr 25 '18 at 12:51