Serve a Frontend
Mount a static directory as an SPA:
from justapi import JustAPIApp
app = JustAPIApp()
@app.frontend("/", directory="dist")
def serve_frontend():
pass
This serves dist/ at / and handles client-side routing (any path not matching an API route returns index.html).
With API Routes
app = JustAPIApp()
@app.get("/api/hello")
def hello():
return {"message": "Hello from API"}
# Must come AFTER API routes
@app.frontend("/", directory="dist")
def serve_frontend():
pass
:::note
Place frontend() after all API routes so API paths take priority.
:::
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
path |
str |
— | Mount path |
directory |
str |
— | Static file directory |
html |
str |
None |
Custom index HTML (overrides file) |
check_dir |
bool |
True |
Raise error if directory doesn't exist |
Custom HTML
@app.frontend("/", html="<h1>My App</h1>")
def serve():
pass
See Also
- Static Files — serving CSS/JS/images
- Sub Applications — Mounts — mounting sub-apps
- Behind a Proxy — reverse proxy config