proxy not working for react and node
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
@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.
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.