I have a static route that works fine, but I would like to add a default document for any/all sub folders - default seems to only be able to take a fixed file to serve, with no way of asking it to look in the request directory.
The following will serve any static file under the "/export" directory when "/export/..." is requested, but if not found then the file "/index.html" is always returned instead of "export/.../index.html".
static("export") {
files("export")
default("index.html")
}
Is there any way to define this? It seems like a standard feature for a web server, so I may have just missed something obvious...
default()
handles only the top-level folder, for whatever reason. You can replace thefiles()
anddefault()
pair with a custom extension, looking like this:Basically, this resolves the local file matching the requested path, serves it if it's a regular file, or serves its
index.html
(if present) if it's a directory. Otherwise, it delegates the error handling to Ktor (likely returning a 404).I wrote up a slightly longer explanation of this solution here as well.