Deprecate app.boot, remove app.installMiddleware
The bootloader has its own project `loopback-boot` now.
This commit is contained in:
parent
ab0700a690
commit
566757ba1d
|
@ -333,6 +333,9 @@ app.enableAuth = function() {
|
|||
/**
|
||||
* Initialize an application from an options object or a set of JSON and JavaScript files.
|
||||
*
|
||||
* **Deprecated. Use the package
|
||||
* [loopback-boot](https://github.com/strongloop/loopback-boot) instead.**
|
||||
*
|
||||
* This function takes an optional argument that is either a string or an object.
|
||||
*
|
||||
* If the argument is a string, then it sets the application root directory based on the string value. Then it:
|
||||
|
@ -691,133 +694,6 @@ function clearHandlerCache(app) {
|
|||
app._handlers = undefined;
|
||||
}
|
||||
|
||||
/*!
|
||||
* This function is now deprecated.
|
||||
* Install all express middleware required by LoopBack.
|
||||
*
|
||||
* It is possible to inject your own middleware by listening on one of the
|
||||
* following events:
|
||||
*
|
||||
* - `middleware:preprocessors` is emitted after all other
|
||||
* request-preprocessing middleware was installed, but before any
|
||||
* request-handling middleware is configured.
|
||||
*
|
||||
* Usage:
|
||||
* ```js
|
||||
* app.once('middleware:preprocessors', function() {
|
||||
* app.use(loopback.limit('5.5mb'))
|
||||
* });
|
||||
* ```
|
||||
* - `middleware:handlers` is emitted when it's time to add your custom
|
||||
* request-handling middleware. Note that you should not install any
|
||||
* express routes at this point (express routes are discussed later).
|
||||
*
|
||||
* Usage:
|
||||
* ```js
|
||||
* app.once('middleware:handlers', function() {
|
||||
* app.use('/admin', adminExpressApp);
|
||||
* app.use('/custom', function(req, res, next) {
|
||||
* res.send(200, { url: req.url });
|
||||
* });
|
||||
* });
|
||||
* ```
|
||||
* - `middleware:error-loggers` is emitted at the end, before the loopback
|
||||
* error handling middleware is installed. This is the point where you
|
||||
* can install your own middleware to log errors.
|
||||
*
|
||||
* Notes:
|
||||
* - The middleware function must take four parameters, otherwise it won't
|
||||
* be called by express.
|
||||
*
|
||||
* - It should also call `next(err)` to let the loopback error handler convert
|
||||
* the error to an HTTP error response.
|
||||
*
|
||||
* Usage:
|
||||
* ```js
|
||||
* var bunyan = require('bunyan');
|
||||
* var log = bunyan.createLogger({name: "myapp"});
|
||||
* app.once('middleware:error-loggers', function() {
|
||||
* app.use(function(err, req, res, next) {
|
||||
* log.error(err);
|
||||
* next(err);
|
||||
* });
|
||||
* });
|
||||
* ```
|
||||
*
|
||||
* Express routes should be added after `installMiddleware` was called.
|
||||
* This way the express router middleware is injected at the right place in the
|
||||
* middleware chain. If you add an express route before calling this function,
|
||||
* bad things will happen: Express will automatically add the router
|
||||
* middleware and since we haven't added request-preprocessing middleware like
|
||||
* cookie & body parser yet, your route handlers will receive raw unprocessed
|
||||
* requests.
|
||||
*
|
||||
* This is the correct order in which to call `app` methods:
|
||||
* ```js
|
||||
* app.boot(__dirname); // optional
|
||||
*
|
||||
* app.installMiddleware();
|
||||
*
|
||||
* // [register your express routes here]
|
||||
*
|
||||
* app.listen();
|
||||
* ```
|
||||
*/
|
||||
app.installMiddleware = function() {
|
||||
var loopback = require('../');
|
||||
|
||||
/*
|
||||
* Request pre-processing
|
||||
*/
|
||||
this.use(loopback.favicon());
|
||||
// TODO(bajtos) refactor to app.get('loggerFormat')
|
||||
var loggerFormat = this.get('env') === 'development' ? 'dev' : 'default';
|
||||
this.use(loopback.logger(loggerFormat));
|
||||
this.use(loopback.cookieParser(this.get('cookieSecret')));
|
||||
this.use(loopback.token({ model: this.models.accessToken }));
|
||||
this.use(loopback.bodyParser());
|
||||
this.use(loopback.methodOverride());
|
||||
|
||||
// Allow the app to install custom preprocessing middleware
|
||||
this.emit('middleware:preprocessors');
|
||||
|
||||
/*
|
||||
* Request handling
|
||||
*/
|
||||
|
||||
// LoopBack REST transport
|
||||
this.use(this.get('restApiRoot') || '/api', loopback.rest());
|
||||
|
||||
// Allow the app to install custom request handling middleware
|
||||
this.emit('middleware:handlers');
|
||||
|
||||
// Let express routes handle requests that were not handled
|
||||
// by any of the middleware registered above.
|
||||
// This way LoopBack REST and API Explorer take precedence over
|
||||
// express routes.
|
||||
this.use(this.router);
|
||||
|
||||
// The static file server should come after all other routes
|
||||
// Every request that goes through the static middleware hits
|
||||
// the file system to check if a file exists.
|
||||
this.use(loopback.static(path.join(__dirname, 'public')));
|
||||
|
||||
// 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.
|
||||
this.use(loopback.urlNotFound());
|
||||
|
||||
/*
|
||||
* Error handling
|
||||
*/
|
||||
|
||||
// Allow the app to install custom error logging middleware
|
||||
this.emit('middleware:error-handlers');
|
||||
|
||||
// The ultimate error handler.
|
||||
this.use(loopback.errorHandler());
|
||||
};
|
||||
|
||||
/**
|
||||
* Listen for connections and update the configured port.
|
||||
*
|
||||
|
|
Loading…
Reference in New Issue