[Python] How to capture arbitrary paths at one route in FastAPI?

Discussão em 'Python' iniciado por Stack, Junho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I'm serving React app from FastAPI by mounting

    app.mount("/static", StaticFiles(directory="static"), name="static")

    @app.route('/session')
    async def renderReactApp(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


    by this React app get served and React routing also works fine at client side but as soon as client reloads on a route which is not defined on server but used in React app FastAPI return not found to fix this I did something as below.

    • @app.route('/network')
    • @app.route('/gat')
    • @app.route('/session')

    async def renderReactApp(request: Request):
    return templates.TemplateResponse("index.html", {"request": request})


    but it seems weird and wrong to me as I need to add every route at the back-end as well as at frontend.

    I'm sure there must be something like Flask @flask_app.add_url_rule('/<path:path>', 'index', index) in FastAPI which will server all arbitrary path

    Continue reading...

Compartilhe esta Página