Implementation of Proxy Server in Angular4-5

懵懂的女人 提交于 2020-01-14 07:02:31

问题


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
   }
}

回答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.



来源:https://stackoverflow.com/questions/50021126/implementation-of-proxy-server-in-angular4-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!