beakr (version 0.3.1)

serveStaticFiles: File-serving middleware

Description

Binds to GET requests that aren't handled by specified paths. The result is to return files that are found on the host machine at the requested path. Binary file types like .png, .gif or .pdf are returned as raw bytes. All others are returned as characters.

Mime types are guessed using the mime package. The rawTypesPattern parameter is used to match mime types that should be returned as raw bytes.

Usage

serveStaticFiles(
  beakr = NULL,
  urlPath = NULL,
  rootPath = getwd(),
  rawTypesPattern = "image|json|octet|pdf|video"
)

Arguments

beakr

Beakr instance or NULL.

urlPath

String representing the URL directory undnerneath which static file paths will appear.

rootPath

String representing the absolute path used as the root directory when searching for files on host machine. Defaults to the directory in which the script is running.

rawTypesPattern

String pattern identifying mime types to be returned as raw bytes.

Value

A Beakr instance with added middleware.

Details

All files to be served in this manner must exist underneath the host machine directory specified with rootPath. The directory structure underneath rootPath will be mapped onto URLs underneath urlPath. This helps when deploying web services at preordained URLs.

For example, specifying:

beakr 
  ...
  serverStaticFiles("/data-service/A", "/data/project_A/public")
  ...
  listen()

will allow a file found on the host machine at:

  /data/project_A/public/2019/averages.csv"

to be downloaded from:

  http://<ip-address>:<port>/data-service/A/2019/averages.csv"