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.
- A boot script discovers models from a DB and exposes them as REST APIs. It needs to be executed before the api-explorer.
- 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:
- Formalize the file names as 01_first.js, 02_second.js for the scaffolded JS files
- Use phases (preferred)
In addition, we probably should move the urlNotFound/errorHandler middleware code to a boot script too.
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.
abc.js (this ensures the file name is sorted before rest-api.js, explorer.js)
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.Potential solutions:
In addition, we probably should move the urlNotFound/errorHandler middleware code to a boot script too.