The most common way to serve files is by using serveDirectory
:
For example:
If the requested path does not map to a file or directory, then serveDirectory
returns mzero
.
If the requested path is a file then the file is served normally using serveFile
.
When a directory is requested, serveDirectory
will first try to find one of the index files (in the order they are listed). If that fails, it will show a directory listing if EnableBrowsing
, otherwise it will return forbidden "Directory index forbidden"
.
The formula for mapping the URL to a file on disk is just what you would expect:
So if the handler is:
And the request URL is:
Then we are going to look for:
The following demo will allow you to browse the directory that the server is running in. (So be careful where you run it).
[Source code for the app is here.]
Simply run it and point your browser at http://localhost:8000/.
The request URL is sanitized so that users can not escape the top-level directory by adding extra .. or / characters to the URL.
The file serving code will follow symlinks. If you do not want that behavior then you will need to roll your own serving function. See the section on Advanced File Serving for more information.