I’m having trouble setting up proxy configuration for my Angular app to communicate with my backend server. I followed the official documentation, but the requests are still not being forwarded properly.
My setup:
- Backend runs on port 3000
- Angular app runs on port 4200
I created a proxy.config.json file in my Angular project:
{
"/service": {
"target": "http://localhost:3000",
"secure": false
}
}
I updated my package.json start script to: "start": "ng serve --proxy-config proxy.config.json"
When I run npm start, I can see the message Proxy created: /service -> http://localhost:3000 in the console.
Here’s my service code that makes the HTTP request:
constructor(private httpClient: HttpClient) {
this.fetchData();
}
fetchData(): void {
this.httpClient.get("/service/users")
.subscribe(result => {
console.log(result);
})
}
But I keep getting http://localhost:4200/service/users 404 (Not Found) error. The request is not being proxied to my backend server at all. What could be wrong with my configuration?
Note: When I access http://localhost:3000/users directly in the browser, it works perfectly fine.