Skip to content

Boot script ordering #61

Description

@raymondfeng

The scripts in server/boot are executed by the order of file names as of today.

There are a few use cases that require ordering.

  1. A boot script discovers models from a DB and exposes them as REST APIs. It needs to be executed before the api-explorer.
  2. An async script that needs to be executed after the system-level boot scripts. For example, if we add the following JS file into server/boot today, the express middleware chain will be messed up:

abc.js (this ensures the file name is sorted before rest-api.js, explorer.js)

module.exports = function(app, cb) {
  process.nextTick(cb);
}

In this case, boot(app, __dirname) returns before the rest-api.js is executed. As a consequence, urlNotFound and errHandler come before the rest handler.

// boot scripts mount components like REST API
boot(app, __dirname);

// -- Mount static files here--
// All static middleware should be registered at the end, as all requests
// passing the static middleware are hitting the file system
// Example:
//   var path = require('path');
//   app.use(loopback.static(path.resolve(__dirname, '../client')));

// Requests that get this far won't be handled
// by any middleware. Convert them into a 404 error
// that will be handled later down the chain.
app.use(loopback.urlNotFound());

// The ultimate error handler.
app.use(loopback.errorHandler());

Potential solutions:

  1. Formalize the file names as 01_first.js, 02_second.js for the scaffolded JS files
  2. Use phases (preferred)

In addition, we probably should move the urlNotFound/errorHandler middleware code to a boot script too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions