-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow frontend dev server to access Tomcat backend
Enables simultaneous development of the frontend code served by Vite and the backend code served by Tomcat. In dev mode: STRCALC_BACKEND='http://localhost:8080/strcalc/' pnpm dev In preview mode (pnpm build && pnpm preview), the STRCALC_BACKEND value will not propagate to the compiled bundle. However, entering the following in the browser console will enable the compiled version to communciate with the backend: globalThis.STRCALC_BACKEND='http://localhost:8080/strcalc/' This required the following steps: - Setting the STRCALC_BACKEND property of `define` in vite.config.js based on the STRCALC_BACKEND environment variable. This sets `globalThis.STRCALC_BACKEND` in the browser environment. - Adding calculators.backendUrl() to determine the URL that backendCalculator() (formerly defaultPost() uses. - Setting the Tomcat CORS filter in the app's web.xml file per: - https://stackoverflow.com/a/18850438 - https://tomcat.apache.org/tomcat-10.1-doc/config/filter.html#CORS_Filter - https://tomcat.apache.org/tomcat-10.1-doc/images/cors-flowchart.png - https://tomcat.apache.org/tomcat-10.1-doc/api/org/apache/catalina/filters/CorsFilter.html Without the CORS configuration, the fetch() from the browser to the backend will fail, and the browser console will show something like: [Error] Origin http://localhost:5173 is not allowed by Access-Control-Allow-Origin. Status code: 200 [Error] Fetch API cannot load http://localhost:8080/strcalc/add due to access control checks. [Error] Failed to load resource: Origin http://localhost:5173 is not allowed by Access-Control-Allow-Origin. Status code: 200 (add, line 0)
- Loading branch information
Showing
5 changed files
with
92 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters