[BUG] Mounting StaticFiles with an APIRouter doesn't work
See original GitHub issueDescribe the bug
Mounting StaticFiles with an APIRouter doesn’t work.
To Reproduce
from typing import Any
from fastapi import FastAPI, Request, APIRouter
from fastapi.staticfiles import StaticFiles
from fastapi.testclient import TestClient
router = APIRouter()
@router.get("/")
async def foo(request: Request) -> Any:
# this raises starlette.routing.NoMatchFound
return request.url_for("static", path="/bar")
app = FastAPI()
router.mount("/static", StaticFiles(directory="."), name="static")
# uncomment to fix
# app.mount("/static", StaticFiles(directory="."), name="static")
app.include_router(router)
client = TestClient(app)
client.get("/")
- Execute the script, raises NoMatchFound
- Uncomment line to mount with app instead
- Executes as expected
Expected behavior
I can use APIRouter() as if it was a FastAPI() as noted in the docs.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:17
- Comments:15 (3 by maintainers)
Top Results From Across the Web
python - How to reuse static files across multiple APIRouters?
This means that I can't reuse my static files across APIRouter s without mounting a new StaticFiles instance against each one, making a...
Read more >Bigger Applications - Multiple Files - FastAPI
The APIRouter s are not "mounted", they are not isolated from the rest of the application. This is because we want to include...
Read more >tiangolo/fastapi - Gitter
Re-declaring the dependency in the path operation level might work, ... Is it possible to mount static files to / root path and...
Read more >Routing - Starlette
If you need a different converter that is not defined, you can create your own. ... StaticFiles(directory="static") # This is a static files...
Read more >Boss machine not running on web browser - Get Help
However, when I run command npm run start, but it did not show up on the web ... Mount your existing apiRouter below...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The underlying problems seems to be that the route does not get applied to the application. The cause is a very restrictive check:
https://github.com/tiangolo/fastapi/blob/f0388915a8b1cd9f3ae2259bace234ac6249c51a/fastapi/routing.py#L713-L721
This should be expanded to work with
routing.BaseRoute
I have different modules with different static things in my app and wanted to separate everything. So that changing the prefix also changes the static prefix. And so they can live in another package not in the same repo.
It’s here https://fastapi.tiangolo.com/tutorial/bigger-applications/#path-operations-with-apirouter “All the same options are supported.”, but I now see that this only refers to path operations.
(btw the search in the docs is broken, it just says “Initializing search” forever)