One of the basic requirements of a web app is the ability to serve static files.Serving static files from a LoopBack application is very simple - just call theapp.static(urlPath, rootDir[, options]) method. The variables in the API areexplained below.

    • app: An instance of a LoopBack application.
    • urlPath: The path where the static assets are to be served from. Refer topath examples in theExpress docs for possible values.
    • rootDir: The directory where the static assets are located on the filesystem.
    • options: An optional object for configuring the underlyingexpress.staticmiddleware.Here is an example of configuring an app to serve the static assets from adirectory named public at /.

    src/application.ts

    1. import * as path from 'path';
    2. export class TodoListApplication extends BootMixin(
    3. ServiceMixin(RepositoryMixin(RestApplication)),
    4. ) {
    5. constructor(options: ApplicationConfig = {}) {
    6. super(options);
    7. // ...
    8. this.static('/', path.join(__dirname, '../../public'));
    9. }
    10. }

    You can call app.static() multiple times to configure the app to serve staticassets from different drectories.

    1. app.static('/files', path.join(__dirname, 'files'));
    2. app.static('/downloads', path.join(__dirname, 'mp3s'));

    You can also call app.static() multiple times on the same mount path to mergefiles from multiple filesystem directories and expose them on the same URL path.When a file with the same name is present in multiple directories mounted at thesame URL path, then the precedence is given the file from the directory that wasregistered earlier.

    1. app.static('/files', path.join(__dirname, 'files'));
    2. app.static('/files', path.join(__dirname, 'other-files'));

    And app.static() can be called even after the app have started.

    1. await app.boot();
    2. await app.start();
    3. app.static('/files', path.join(__dirname, 'files'));