serveFileis an async function that will search for a file on your filesysten and produce a response for it
Implemented in src/file-service/serve-file.js, you can use it as shown below.
import { serveFile } from "@dmail/server"
const response = await serveFile("/Users/you/folder/index.html", {
method: "GET",
headers: {
"if-modified-since": "Wed, 21 Oct 2015 07:28:00 GMT",
},
cacheStrategy: "mtime",
})serveFile was designe to produce a response and called inside requestToResponse like this:
import { serveFile, startServer } from "@dmail/server"
startServer({
requestToResponse: ({ ressource, methods, headers }) =>
serveFile(`${__dirname}${ressource}`, {
method,
headers,
}),
})
pathis the first parameter of serveFile, it is a string leading to a filesystem entry.
methodis a string representing an http request method.
This parameter is optional with a default value of
"GET"When method is not HEAD or GET the returned response correspond to 501 not implemented.
headersis an object representing http request headers.
This parameter is optional with a default value of
{}Two header might be checked in this optionnal object: if-modified-since and if-none-match. They will be checked according to cacheStrategy below.
cacheStrategyis a string controlling if server checkheadersparameter and add headers for cache in response.
This parameter is optional with a default value of
"etag"When "mtime": response will contain "last-modified" header
When "etag": response will contain "etag" header
When "none": response will contain "cache-control": "no-store" header
contentTypeMapis an object used to get content type from an url.
This parameter is optional with a default value coming from file-service/content-type-map.js.
You can extend contentTypeMap to add or replace contentTypes mapping like this:
import { serveFile } from "@dmail/server"
const response = await serveFile("/Users/you/folder/index.html", {
method: "GET",
contentTypeMap: {
...defaultContentTypeMap,
"application/x-whatever": {
extensions: ["whatever", "whatever-2"],
},
},
})
canReadDirectoryis a boolean indicating if reading a directory is allowed.
This parameter is optional with a default value of
falseWhen false serveFile respond with 403 not allowed to read directory when path leads to a directory on your filesystem.
When true serveFile respond with 200 and response is a json string being an array of filenames inside the directory.