Example that serves static files from a directory

This commit is contained in:
Miguel Grinberg
2022-07-31 18:51:49 +01:00
parent 16f3775fa2
commit a3d7772b8a
3 changed files with 29 additions and 0 deletions

19
examples/static.py Normal file
View File

@@ -0,0 +1,19 @@
from microdot import Microdot, send_file
app = Microdot()
@app.route('/')
def index(request):
return send_file('static/index.html')
@app.route('/static/<path:path>')
def static(request, path):
if '..' in path:
# directory traversal is not allowed
return 'Not found', 404
return send_file('static/' + path)
app.run(debug=True)

View File

@@ -0,0 +1,10 @@
<!doctype html>
<html>
<head>
<title>Static File Serving Demo</title>
</head>
<body>
<h1>Static File Serving Demo</h1>
<img src="static/logo.png" alt="logo">
</body>
</html>

BIN
examples/static/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB