question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Mounting StaticFiles with an APIRouter doesn't work

See original GitHub issue

Describe 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:open
  • Created 3 years ago
  • Reactions:17
  • Comments:15 (3 by maintainers)

github_iconTop GitHub Comments

8reactions
septatrixcommented, Jan 16, 2022

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

5reactions
lazkacommented, May 24, 2020

If there is a specific need to include static files under a router, could you outline why that feature would be more useful then the basic mounting on the app?

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.

Could you also point out the specific docs that were confusing so they can be clarified?

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)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found