I am a bit of a beginner to React. I developed a Flask backend and now i want to pair it with React for frontend.
I am using fetch in React to make the GET Request. When i read the data, the text or the response when i call response.text() is the index.html file in the public directory of my app
Here is my react code:
componentDidMount() {
fetch('/')
.then(response => {
console.log(response.text()) //Here is the text() i said before
this.setState({ snippets: response.data })
})
.catch(error=>{
console.log(error)
})
}
Here is the MRE of my flask app:
@app.route('/')
def index():
return {'snippets':['blah','blaha']
My proxy in package.json
"proxy": "http://127.0.0.1:5000/"
My flask backend is running at port 5000 and react at port 3000
One thing to note is that a POST request (from <form>) does get proxied to the backend server and i can retrieve the contents of the POST request in flask. Its the GET request using fetch that isn't working.
Directory structure:
-env
-getcode
-templates
-static
-__init__.py
-routes.py
-getcode-client
-src
-public
run.py
Here getcode is the directory of the flask app and getcode-client contains the React app created using create-react-app
NOTE: Also i tried to setup a manual proxy like this: https://create-react-app.dev/docs/proxying-api-requests-in-development/#configuring-the-proxy-manually
but now the react app is not shown. it completely shows my flask backend's json output.
Package.json:
{
"name": "getcode-client",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.3.2",
"@testing-library/user-event": "^7.1.2",
"axios": "^0.19.2",
"http-proxy-middleware": "^1.0.3",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}