2018-08-29 10:17:09 +00:00
|
|
|
|
2018-11-08 09:33:20 +00:00
|
|
|
Starlette also includes a `StaticFiles` class for serving files in a given directory:
|
2018-08-29 10:17:09 +00:00
|
|
|
|
2018-11-08 09:33:20 +00:00
|
|
|
### StaticFiles
|
|
|
|
|
|
|
|
Signature: `StaticFiles(directory, check_dir=True)`
|
|
|
|
|
|
|
|
* `directory` - A string denoting the directory path
|
|
|
|
* `check_dir` - Ensure that the directory exists upon instantiation. Defaults to `True`
|
2018-08-29 10:17:09 +00:00
|
|
|
|
2018-10-28 18:04:17 +00:00
|
|
|
You can combine this ASGI application with Starlette's routing to provide
|
2018-08-29 10:17:09 +00:00
|
|
|
comprehensive static file serving.
|
|
|
|
|
|
|
|
```python
|
2018-10-30 13:03:40 +00:00
|
|
|
from starlette.routing import Router, Mount
|
2018-10-28 18:04:17 +00:00
|
|
|
from starlette.staticfiles import StaticFiles
|
2018-08-29 10:17:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
app = Router(routes=[
|
2018-10-30 13:03:40 +00:00
|
|
|
Mount('/static', app=StaticFiles(directory='static')),
|
2018-08-29 10:17:09 +00:00
|
|
|
])
|
|
|
|
```
|
|
|
|
|
2018-08-30 13:42:39 +00:00
|
|
|
Static files will respond with "404 Not found" or "405 Method not allowed"
|
2018-08-29 10:17:09 +00:00
|
|
|
responses for requests which do not match.
|