Skip to main content
Version: Next

File variants

Variants of type file define a file to transfer when a route is requested. It sets the Content-Type response HTTP header field based on the filename's extension.

Options

The options property in a variant of type file must be an object containing next properties:

  • path (String): Path to the file to be sent. It can be an absolute path, or a relative path from process.cwd, unless the root option is set in the options object described bellow.
  • headers (Object): Object containing headers to set in the response. Optional.
  • options (Object): Object containing any of the available express.sendFile method options. Some of them are:
    • root (String): Root directory for relative filenames. The path option must be relative to this path, or absolute. Default is the value of process.cwd().
    • maxAge (Number) : Set the max-age property of the Cache-Control header in milliseconds or a string in ms format.
    • ... : Any of the other options supported by the express.sendFile method.

For example, use the following route to send the content of the users.json file as response for the path /api/users:

module.exports = [
{
id: "get-users",
url: "/api/users",
variants: [
{
id: "success",
type: "file",
options: {
status: 200, // Status to be sent
path: "mocks/files/users.json", // path of the file to be transferred
options: { // options for the express.sendFile method
maxAge: 500
}
},
}
]
}
];
tip

The Content-Type response HTTP header field is set automatically based on the filename's extension, but you can still use the headers option to set your own headers or override the automatic header.