proxy not working for react and node
I'm having issues with the proxy I set up.
This is my root package.json file:
"scripts": {
"client": "cd client && yarn dev-server",
"server": "nodemon server.js",
"dev": "concurrently --kill-others-on-fail \"yarn server\" \"yarn client\""
}
My client package.json file:
"scripts": {
"serve": "live-server public/",
"build": "webpack",
"dev-server": "webpack-dev-server"
},
"proxy": "http://localhost:5000/"
I've set up express on my server side to run on port 5000. Whenever I make a request to the server, ie :
callApi = async () => {
const response = await fetch('/api/hello');
const body = await response.json();
// ... more stuff
}
The request always goes to
Can someone point out what i have to do to fix this issue so that the request actually goes to port 5000?
I experienced this issue quite a lot of times, and I figured it's because of the cache. To solve the issue, do the following
Edit: @mkoe said that he was able to solve this issue simply by deleting the package-lock.json file, and restarting the app, so give that a try first. If that doesn't resolve it, then do the following.
- Stop your React app
- Delete package-lock.json file and the node_modules directory by doing
rm -r package-lock.json node_modules
in the app directory. - Then do
npm install
in the app directory.
Now your proxy in the package.json will not have any issues.
The reason the react application is still pointing at localhost:8080 is because of cache. To clear it , follow the steps below.
- Delete
package-lock.json
andnode_modules
in React app- Turn off React Terminal and
npm install
all dependencies again on React App- Turn back on React App and the proxy should now be working
This problem has been haunting me for a long time; but if you follow the steps above it should get your React application pointing at the server correctly.
This is how I achieved the proxy calls.
- Do not rely on the browser's network tab. Put consoles in your server controllers to really check whether the call is being made or not. For me I was able to see logs at the server-side. My node server is running on 5000 and client is running on 3000.
Network tab -
Server logs -
- Check if your server is really running on the same path
/api/hello
through postman or browser. For me it was/api/user/register
and I was trying to hit/api/user
- Use cors package to disable cross-origin access issues.
Is your client being loaded from http://localhost:8080
?
By default the fetch
api, when used without an absolute URL, will mirror the host of the client page (that is, the hostname and port). So calling fetch('/api/hello');
from a page running at http://localhost:8080
will cause the fetch
api to infer that you want the request to be made to the absolute url of http://localhost:8080/api/hello
.
You will need to specify an absolute URL if you want to change the port like that. In your case that would be fetch('http://localhost:5000/api/hello');
, although you probably want to dynamically build it since eventually you won't be running on localhost
for production.
For me "proxy" = "http://localhost:5000
did not work because I was listening on 0.0.0.0
changing it to "proxy" = "http://0.0.0.0:5000
did work.