Managing the CORS mechanism
When integrating API endpoints with various frontend frameworks, we often encounter the "no ‘access-control-allow-origin’ header present" error from our browser. Nowadays, this setup is an HTTP-header-based mechanism of any browser, which requires the backend server to provide the browser with the "origin" details of the server-side application, which includes the server domain, scheme, and port. This mechanism is called CORS, which happens when the frontend application and its web resources belong to a different domain area than the backend app. Nowadays, browsers prohibit cross-origin requests between the server-side and frontend applications for security reasons.
To resolve this issue, we need our main.py
module to place all the origins of our application and other integrated resources used by the prototype inside a List
. Then, we import the built-in CORSMiddleware
from the fastapi.middleware.cors
module and add that...