I am working on a web application with a create-react-app frontend and a Flask API backend with Flask-RESTful. I built the app locally and it works on my computer, but now I am unsuccessfully trying to deploy it to Heroku.
I deployed the Flask backend and the React frontend as two different Heroku dynos, so each one has their own URL. When I go to the URL for the frontend, it appears to be working fine, and the backend URL seems to work too. However, the app relies on API calls to the backend, and I am having trouble successfully sending requests to my backend.
When I was working locally, I would run the flask backend with the gunicorn --bind 0.0.0.0:8000 app:app
and the frontend with the npm start
command. The app worked fine with relative calls in my Axios function calls as long as I put "proxy": "https://localhost:8000"
in my package.json
file in my frontend app. In general, I followed this tutorial: https://www.fullstackreact.com/articles/using-create-react-app-with-a-server/.
I thought I could simply edit the proxy
field in the package.json
file to point to the URL of my backend Heroku app, and then I wouldn't have to change anything else. However, when I deploy using this, I find that the requests to my endpoint are being made to https://my-frontend-app.herokuapp.com/<endpoint>
instead of https://my-backend-app.herokuapp.com/<endpoint>
.
Here is my package.json:
{
"name": "heroku-app",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"bootstrap": "^4.1.2",
"plotly.js": "^1.39.2",
"react": "^16.4.1",
"react-dom": "^16.4.1",
"react-plotly.js": "^2.2.0",
"react-scripts": "1.1.4",
"reactstrap": "^6.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"proxy": "https://star-employees-api.herokuapp.com"
}
My requests look like this:
axios.get('/analyze', data)
.then((response) => {
// Handle request response here
};
Can anyone tell me what I'm doing wrong here? This is my first time deploying an app like this and using Heroku, so I'm not sure if I'm way off base here. If there is a better way to deploy this kind of app, please let me know as well. I can provide any other code snippets or file structure if it's helpful, just not sure what would be helpful at this point. Thanks in advance.