Creates an instance of this type that serves files from pathOfDirectoryToServe
.
An instance of this type serves files by appending all or part of an HTTP request path to pathOfDirectoryToServe
and streaming the bytes
of that file as a response.
Instances of this type are piped from a route that MUST contain the match-all route pattern (*
). For example, consider the following:
router
.route("/site/*")
.pipe(new HTTPFileController("build/web"));
In the above, GET /site/index.html
would respond with the contents of the file build/web/index.html
, relative to the project directory.
If pathOfDirectoryToServe
contains a leading slash, it is an absolute path. Otherwise, it is relative to the current working directory
of the running application.
If no file is found, the default behavior is to return a 404 Not Found. (If the Request accepts 'text/html', a simple 404 page is returned.) You may
override this behavior by providing onFileNotFound
. The first argument to [onFileNotFound is the instance of this type that could not find the file and
may be accessed to use any settings like cache policies or content type mappings. The second argument is the request.
The content type of the response is determined by the file extension of the served file. There are many built-in extension-to-content-type mappings and you may
add more with setContentTypeForExtension. Unknown file extension will result in application/octet-stream
content-type responses.
The contents of a file will be compressed with 'gzip' if the request allows for it and the content-type of the file can be compressed according to HTTPCodecRepository.
Note that the 'Last-Modified' header is always applied to a response served from this instance.
Source
HTTPFileController(String pathOfDirectoryToServe, {Future<Response> onFileNotFound(HTTPFileController controller, Request req)}) : _servingDirectory = new Uri.directory(pathOfDirectoryToServe), _onFileNotFound = onFileNotFound;