Skip to content

Latest commit

 

History

History
129 lines (81 loc) · 3.18 KB

static-configurator.md

File metadata and controls

129 lines (81 loc) · 3.18 KB

StaticConfigurator

The StaticConfigurator class provides methods to register static resources in the server as well as an index page. An instance of this configurator can be found with the Configurator.prototype.static property:

const server = new Configurator();

// *Getting the static configurator:
let static_configurator = server.static;



Properties

StaticConfigurator.prototype.index_file

string - The index file path (absolute)

Note: readonly, use the index() method to assign a value.



StaticConfigurator.prototype.resources

Array<{ route, path }> - An array containing all the static resources

  • route string - The server route
  • path string - The absolute path to a file/directory in disk

Note: readonly, use the add() method to add a new resource.




Methods

StaticConfigurator.prototype.index(file, options)

Sets the main HTML file

Note: By default, the given file will be served only on the root route (i.e. http://localhost/).

Parameters

  • file string - The relative/absolute file path
  • options object - [optional] Aditional options, the following properties can be set:
  • options.root_only boolean - It sets whether the index file should be served only at the root route ('/'), or at all available routes (initial value is true)

Returns

StaticConfigurator - This same configurator (for method chaining)

Throws

  • TypeError - If the file is not a string
  • Error - If the file does not represent a path to a file

Examples

server.static
   .index('../src/my_index.html', { root_only: true });


StaticConfigurator.prototype.add(route, resource_path, options)

Registers a static directory or file to be served on the given route

Parameters

  • route string - The server route
  • resource_path string - The relative/absolute file/directory path
  • options object - The static options object
  • options.dotfiles string - Express static dotfiles property
  • options.maxAge number - Express static maxAge property
  • options.etag boolean - Express static etag property

Returns

StaticConfigurator - This same configurator (for method chaining)

Throws

  • TypeError - If the resource path is not a string

Examples

server.static
   .add('/static/js', '../src/js')
   .add('/static/css', '../src/css', { dotfiles: 'allow' })
   .add('/static/logo', '../src/images/logo.png');


StaticConfigurator.prototype.done()

Retrieves the main configurator

Returns

Configurator - The main configurator (for configurator chaining)

Examples

// *Main 'Configurator' object:
server
   .port(...)
   .static
      // *Inner 'StaticConfigurator' object:
      .index(...)
      .add(...)
      .add(...)
      .done()   // Returns the chain back to the main configurator
   // *Main 'Configurator' object:
   .start();