I created a project using create-react-app and this one is running on http://localhost:3000/
when i want make a request to http://localhost:3090/ from my application i am setting proxy in my package.json file which is not working
componentDidMount() {
fetch('/api/si')
.then(response => {
console.log(response);
return response.json();
})
}
package.json
"proxy":"http://localhost:3090/api"
here my expected call is localhost:3090/api/si but it's pointing to 3000 which is my client server. I tried multiple combinations nothing works
Try Removing /api from
"proxy":"http://localhost:3090/api"
or else try
componentDidMount() {
fetch('/si')
.then(response => {
console.log(response);
return response.json();
})
}
Change one of them
In case of Multiple Proxies you can do something like this:
"proxy": {
"/auth/*": {
"target": "http://localhost:5000"
},
"/api/*": {
"target": "http://localhost:3090"
}
}
The fix in my case was to put "proxy" below "scripts" in react-app package.json file. Initially I was putting it at the end of file below "development" tag. I don't know how it worked but it did. This is what I did:
My package.json file which was working is:
"name": "client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.14.1",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.8.3",
"axios": "^0.21.1",
"react": "^17.0.2",
"react-bootstrap": "^1.6.1",
"react-dom": "^17.0.2",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"web-vitals": "^1.1.2"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"proxy": "http://localhost:5000/",
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With