Purpose: In this exercise, you will connect the React App you made in Exercise 5.3 with the Flask API Backend you made in Exercise 4.2, converting to AJAX using the skills you learned in Exercise 4.4 and Project Part 4.5. You will first do this using the React Dev Server and the Flask Dev Server. Then, you will make the changes needed to enable the code to run in React built mode, in Flask CGI mode, and finally on the webhost.
Look at the directory structure in Project Part 5 on the Project Directory Structure page in Canvas. You may notice that the project root has a reactapp/ and flaskapp/ directory. It is a common tactic to organize source code in this way. Copy all your source code from Exercise 5.3 into reactapp/and all your source code from Exercise 4.2 into flaskapp. Your directory structure may look as follows:
/
|---reactapp/
| |---index.html
| |---(react config files)
| |---src/
| | |---(component files)
| |---public/
| |---(images)
|---flaskapp/
|---app.py
|---static/
| |---(static files)
|---templates/
| |---(jinja templates, soon to be deleted)
|---uploads/
|---(user uploaded images)
First, clean up: Now that React takes care of all UI routes, remove all routes except the "/alert" route (POST method). Remove the entire templates directory.
Second, convert: As in Exercise 4.4, convert server responses from UI to API by removing render_template and returning simple text messages "err_missing", "err_filetype", and "ok". Take the content from the "message = " lines and prepare to move them to the React App.
Make BatForm.jsx send an AJAX request. To separate concerns, put the AJAX functionality (all uses of fetch or the back end URL) in its own JS file (api.js).
Test the form submission using the React Dev Server and the Flask Dev Server. Oh no! It fails! Are villains jamming the Bat signal? No! It's only the CORS policy (Cross Origin Resource Sharing).
The issue here is that now we are running two separate servers. The Flask Server, running on localhost:5000, sees that the request originated from localhost:5173, which is considered a different server. There is information about this in the HTTP headers, because Browsers are designed to put that information there. Although that information can be manipulated and spoofed easily enough, the most proper way to deal with it is to configure the server's CORS policy to recognize the requests. To do this:
Install Flask's CORS-handling library:
and import into app.py
and underneath app = Flask(__name__), add:
This allows our API to receive requests from anywhere. If we wanted to restrict so that only requests from localhost:5000 are allowed, we could use
instead, a much more restrictive policy. However, it is not altogether uncommon for APIs to be made as public as webpages, especially when multiple front-end interfaces are desired, so for simplicity's sake we will make public CORS policies in this class.
As it happens, in this class, we only run into CORS problems in this class in test mode, when we are running dev servers. If we run both on XAMPP, there will be no CORS problem, and if we run both on the Webhost, there will be no CORS problem, because the React AJAX requests originate from same server as the Flask API, even though they are mounted in different directories. However, plan on using flask-cors when you submit your project parts, because your code is required to run on localhost or webhost, dev mode or production mode.