Static file assets

The most flexible way to serve files in your Foxx service is to simply passthem through in your router using thecontext object’s fileName method and theresponse object’s sendFile method:

  1. router.get('/some/filename.png', function (req, res) {
  2. const filePath = module.context.fileName('some-local-filename.png');
  3. res.sendFile(filePath);
  4. });

While allowing for greater control of how the file should be sent to the client and who should be able to access it, doing this for all your static assets can get tedious.

Alternatively you can specify file assets that should be served by your Foxx service directly in the service manifest using the files attribute:

  1. "files": {
  2. "/some/filename.png": {
  3. "path": "some-local-filename.png",
  4. "type": "image/png",
  5. "gzip": false
  6. },
  7. "/favicon.ico": "bookmark.ico",
  8. "/static": "my-assets-folder"
  9. }

Each entry in the files attribute can represent either a single file or a directory. When serving entire directories, the key acts as a prefix and requests to that prefix will be resolved within the given directory.

Options

  • path: string

The relative path of the file or folder within the service.

  • type: string (optional)

The MIME content type of the file. Defaults to an intelligent guess based on the filename’s extension.

  • gzip: boolean (Default: false)

If set to true the file will be served with gzip-encoding if supported by the client. This can be useful when serving text files like client-side JavaScript, CSS or HTML.

If a string is provided instead of an object, it will be interpreted as the path option.