Exercise 5.3: React + Flask in Prod Mode - Localhost and Webhost

Purpose: To learn to move your React App and Flask App between Dev Mode and Production Mode in the separate servers approach.

The separate-servers approach is the most popular way to connect a React front-end to a Flask back-end, because it is good both for separation of concerns and for migration to cloud platforms. Issues arise when we move from dev mode to production mode. This exercise is designed to get you familiar with them and help solve them.

Switching Between Dev and Prod Modes on Localhost

Before we deploy to webhost, we should get comfortable running our app with or without dev servers, in the four scenarios shown in the table below:

React Dev Mode React Prod Mode
Flask Dev Mode React Dev Server (localhost:5173)
connects to:
Flask Dev Server (localhost:5000)
React Dist Folder (through XAMPP)
connects to:
Flask Dev Server (localhost:5000)
Flask Prod Mode React Dev Server (localhost:5173)
connects to:
Flask CGI (through XAMPP)
React Dist Folder (through XAMPP)
connects to:
Flask CGI (through XAMPP)

There are specific issues that arise when we move React to Prod Mode and Flask to CGI mode, described below:

Flask Prod Mode and URL-Pointing Problem

For now, keep your React code in dev mode, but run your Flask code in prod mode (CGI, since we are on localhost). If needed, brush up on running Flask in CGI mode as taught in Exercise 2.2. Copy in the needed index.cgi and .htaccess files along with your Flask code into a subdirectory of your htdocs folder, such as "htdocs/react_batpage_api". Your Flask server endpoint should now run on "localhost/react_batpage_api". Test that it works by going to this URL (it should produce "err_missing", since you have no post data). If you want to test your Flask API independently of React, you can download Postman and use it as a monkey patch. Or you can just use your React code to test it as shown below.

It is fairly easy to change your React code to point to the new location. Simply go into BatForm.jsx and change as follows:

const flaskurl = "http://localhost:5000";

to:

const flaskurl = "http://localhost/react_batpage_api";

However, making this change causes your code stop working when you run the Flask Dev Server. You must comment out the line not being used. While this type of setup is acceptable while you are testing your code, it will not work when you turn in your project, which must be robust enough to run on localhost (at least in dev mode for both servers) and run on webhost (in prod mode for both servers).

There is a global variable in Vite projects called import.meta.env that you can use to help you switch your flaskurl as needed. This global variable does the following:

React Prod Mode and React Router URL Issues

When you run your React in Prod Mode, URL issues arise in React Router. You need to get familiar with them and learn to fix them, because Prod Mode is required to deploy to Webhost. The steps below guide you through seeing and solving the problems on localhost.

Step 1. Move to React Prod Mode Move React to Prod Mode by running pnpm run build and putting the dist folder somehwere in htdocs, your XAMPP document root. Run it in a subdirectory, such as htdocs/react_batpage or something similar. Remember that your vite.config.js file must be configured properly to run in a directory other than document root, as taught in Exercise 5.1.

Step 2. Experience React Router URL Bar Problems: Although your project should run and the links should work at this point, look at the URL bar. You should notice that the URLs generate wrong, if you are running in a subdirectory as instructed. React Router, which is controlling the URL bar, acts as though the project is mounted at document root, not in a subdirectory. This can create problems for your user experience, especially if they try to bookmark or save links (try to refresh from URL bar to see). Two fixes are required to solve this problem, one that fixes what the URL Bar says and the other that fixes what the URL Bar does.

Step 3. Fix What the URL Bar Says: To fix the URL bar emulation, change main.jsx so the BrowserRouter knows where to point its basename. If your base URL should be localhost/react_batpage, you can set the following prop in the BrowserRouter component:

<BrowserRouter basename="/react_batpage">

However, this creates a problem if you are running on the dev server, since that is not the correct location. You can calculate a good relative path using window.location.pathname and put it into thet BrowserRouter basename prop, as follows:

const lastslash = window.location.pathname.lastIndexOf("/");
const basename = window.location.pathname.substring(0, lastslash);

...

<BrowserRouter basename={basename}>

Note: The above code relies on the fact that because general webservers like Apache and Litespeed take care of the trailing slash problem, which comes up whenever Phusion Passenger is used (but your React code does not use Phusion Passenger like your Flask code does). Alternatively, you can consolidate it into a regex:

<BrowserRouter basename={window.location.pathname.replace(/(\/[^/]+)$/, "")}>

Note also: The above code relies on your React page structure being fairly flat (no subdirectories within it). If you use a more complex structure, you can either add complication to the above code or use a server-dependent .env file. In this class, there is a preference for deployable-anywhere code, but server-dependent .env files are acceptable so long as they make the code work in all required situations (localhost-dev and webhost-prod).

Step 4. Fix What the URL Bar Does: Now that the URL bar generates correctly, one final problem remains to make React Router work right in prod mode. When you click on links such as "localhost/react_batpage/about", XAMPP will look for a file or directory called "htdocs/react_batpage/about". We don't want it to do that, since every URL in a React Router situation should route through index.html. An easy solution is to put the .htaccess file below in your dist folder to help XAMPP or Litespeed run everything in the React dist folder through index.html:

.htaccess
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]

Deploying to the Webhost

The purpose of this subsection is to get you used to the webhost file structure. On the webhost, we only use prod mode for both Flask and React, since our webhost restricts the use of ports 5000 and 5173.

Follow the steps on the Project Deployment and Submission instructions in Canvas under "Deploying a React+Flask Project", with the following changes. The below instructions assume your subdomain is student1.cs3660.xyz; adjust as needed:

Test your project and make sure it works.

Be Aware of Alternative Approaches to Deployment

The following methods are not taught in this class but are perfectly possible and may be used in some working environments. Be aware of them. Practice them if desired, but do not use them on the project.

(1) Not Separating Servers. We separate the servers chiefly because it is an increasingly popular approach to separate concerns into microservices, particularly in Cloud Computing. However, this approach may not always be desirable. There are two main ways to deploy a less separated, more merged React and Flask app:

(2) Building React on Both Hosts. Some companies run "pnpm run build" or the equivalent on both local and remote hosts. We avoid this mainly to spare you the complication of building node projects on our webhost.

(3) Not Matching Dev and Prod Code. There are many ways to use non-matching dev and prod code, although we do not recommend them.

(4) Not Making Code Deployable Anywhere. Although we have a preference to make code deployable anywhere, this is not always desired. It is common to use .env files or the equivalent to set up variables that enable projects to run on different platforms.