Server.Static
Serve static files from a provided directory
Configuration options required to find files and generate responses.
fileSystemPermission
is the permission you get from theFileSystem
module of thegren-lang/node
package for accessing the file system. This module needs read access to the file system to get any requested files.httpResponse
is theResponse
type you get from theHttpResponse
module of thegren-lang/node
package. This is required for this module to construct and return an HTTPResponse
.directory
is the directory that will be looked into to find any requested files. This module will only search the file system for files in this directory.mode
describes how this module will search for and find static files. Check out the docs for theMode
type for more details.
The Mode
dictates what files will be searched for and in what order. Modes are meant to
support common use-cases without any configuration. If there's a use-case this does not
support, leave feedback on Github!
Normal
Mode
Normal
mode attempts to find a file with the exact path given. If a file is not found, it
fails with a 404 Response
.
SinglePageApp
Mode
SinglePageApp
mode attempts to find the file on the given path. If it fails, it looks for an
index.html
file at the root of the static file directory and returns it instead. If there
is no such file, it fails and with a 404 Response
.
As the mode name suggests, this allows for serving single-page apps which have a single
index.html
file for the entire application and want to route most (if not all) traffic
through it.
PrettyUrl
Mode
PrettyUrl
mode attempts to find the file on the given path. If it fails, append index.html
to the given path to try and find that file. If this fails, return a 404 Response
.
This mode supports many static-site generators who generate urls like about/index.html
rather than
about.html
for each generated file.
Try to get a file from a folder given a passed Url
.
This function takes a Url
and uses its path to know what file to look for. This decision
makes it simple to integrate with received requests from the HttpServer
module of the
gren/node
package.
When the task is successful, this function will produce a Response
that contains the
requested files contents as a String
. If the task fails, it will produce an error.
Try to get a file from a folder given a passed Url
.
This function just maps the error produced by the response
function, ignores is, and
uses a 404 response in its place. It's implementation is simple and can be copied or
modified to suite your needs.
response config url
|> Task.mapError (\_ -> HttpServer.Response.setStatus 404 config.httpResponse)