Skip to content

Taxum / @taxum/fs / ServeDir

Class: ServeDir

Defined in: serve-dir.ts:42

Service that serves files from a given directory and all its subdirectories.

The Content-Type will be guessed from the file extension.

An empty response with status 404 Not Found will be returned if:

  • The file doesn't exist
  • Any segment of the path contains ..
  • Any segment of the path contains a backslash
  • Any segment of the path referenced as directory is actually an existing file (/file.html/something)
  • We don't have necessary permissions to read the file

Example

ts
import { ServeDir } from "@taxum/fs";

// This will serve files from the "assets" directory and its subdirectories
const service = new ServeDir("assets");

Implements

Constructors

Constructor

new ServeDir(base): ServeDir

Defined in: serve-dir.ts:52

Creates a new ServeDir.

Parameters

base

string

Returns

ServeDir

Methods

appendIndexHtmlOnDirectories()

appendIndexHtmlOnDirectories(append): this

Defined in: serve-dir.ts:79

If the requested path is a directory, append index.html.

This is useful for static sites.

Defaults to true.

Parameters

append

boolean

Returns

this


callFallbackOnMethodNotAllowed()

callFallbackOnMethodNotAllowed(callFallback): this

Defined in: serve-dir.ts:219

Customizes whether to call the fallback for requests that aren't GET or HEAD.

Defaults to not calling the fallback and instead returning 405 Method Not Allowed.

Parameters

callFallback

boolean

Returns

this


fallback()

fallback(fallback): this

Defined in: serve-dir.ts:185

Sets the fallback service.

This service will be called if there is no file at the path of the request.

The status code returned by the fallback will not be altered. Use notFoundService to set a fallback and always respond with a 404 Not Found.

Parameters

fallback

HttpService

Returns

this

Example

ts
import {ServeDir, ServeFile} from "@taxum/fs";

const service = new ServeDir("assets")
    // respond with a `not_found.html` for missing files
    .fallback(new ServeFile("assets/not_found.html"));

invoke()

invoke(req): Promise<HttpResponse>

Defined in: serve-dir.ts:224

Process the request and return the response asynchronously.

Parameters

req

HttpRequest

Returns

Promise<HttpResponse>

Implementation of

Service.invoke


notFoundService()

notFoundService(fallback): this

Defined in: serve-dir.ts:207

Sets the fallback service and overrides the fallback's status code to 404 Not Found.

This service will be called if there is no file at the path of the request.

Parameters

fallback

HttpService

Returns

this

Example

ts
import {ServeDir, ServeFile} from "@taxum/fs";

const service = new ServeDir("assets")
    // respond with `404 Not Found` and the contents of
    // `not_found.html` for missing files
    .notFoundService(new ServeFile("assets/not_found.html"));

precompressedBr()

precompressedBr(): this

Defined in: serve-dir.ts:121

Informs the service that it should also look for a precompressed brotli version of any file in the directory.

Assuming the dir directory is being served and dir/foo.txt is requested, a client with an Accept-Encoding header that allows the brotli encoding will receive the file dir/foo.txt.br instead of dir/foo.txt.

If the precompressed file is not available, or the client doesn't support it, the uncompressed version will be served instead. Both the precompressed version and the uncompressed version are expected to be present in the directory. Different precompressed variants can be combined.

Returns

this


precompressedDeflate()

precompressedDeflate(): this

Defined in: serve-dir.ts:141

Informs the service that it should also look for a precompressed deflate version of any file in the directory.

Assuming the dir directory is being served and dir/foo.txt is requested, a client with an Accept-Encoding header that allows the deflate encoding will receive the file dir/foo.txt.zz instead of dir/foo.txt.

If the precompressed file is not available, or the client doesn't support it, the uncompressed version will be served instead. Both the precompressed version and the uncompressed version are expected to be present in the directory. Different precompressed variants can be combined.

Returns

this


precompressedGzip()

precompressedGzip(): this

Defined in: serve-dir.ts:101

Informs the service that it should also look for a precompressed gzip version of any file in the directory.

Assuming the dir directory is being served and dir/foo.txt is requested, a client with an Accept-Encoding header that allows the gzip encoding will receive the file dir/foo.txt.gz instead of dir/foo.txt.

If the precompressed file is not available, or the client doesn't support it, the uncompressed version will be served instead. Both the precompressed version and the uncompressed version are expected to be present in the directory. Different precompressed variants can be combined.

Returns

this


precompressedZstd()

precompressedZstd(): this

Defined in: serve-dir.ts:161

Informs the service that it should also look for a precompressed zstd version of any file in the directory.

Assuming the dir directory is being served and dir/foo.txt is requested, a client with an Accept-Encoding header that allows the deflate encoding will receive the file dir/foo.txt.zst instead of dir/foo.txt.

If the precompressed file is not available, or the client doesn't support it, the uncompressed version will be served instead. Both the precompressed version and the uncompressed version are expected to be present in the directory. Different precompressed variants can be combined.

Returns

this