FastAPI middleware and CORS
The concept of middleware is common in almost every reputable web framework, and FastAPI is no exception. Middleware is just a function that accepts requests before they are handed over to the path operations for processing and also responds before they are returned.
This simple concept is quite powerful and has many uses—a middleware can check for specific headers that contain authentication data (such as a bearer token) and accept or deny requests accordingly, it can be used for rate limiting (often with the Redis key-value database), and so on.
Creating middleware in FastAPI is based on Starlette’s middleware, like most web-related concepts in FastAPI, and the documentation provides some nice examples: https://fastapi.tiangolo.com/tutorial/middleware/.
In your application, you will use a ready-made middleware to enable the FastAPI-based backend—which will be running on one machine—to communicate with a frontend (in...