问题
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