Set up middleware in middleware.json.
Here is the default version created by the Application generator:
{
"initial:before": {
"loopback#favicon": {}
},
"initial": {
"compression": {},
"cors": {
"params": {
"origin": true,
"credentials": true,
"maxAge": 86400
}
}
},
"session": {},
"auth": {},
"parse": {},
"routes": {
"loopback#rest": {
"paths": [
"${restApiRoot}"
]
}
},
"files": {
"loopback#static": {
"params": "$!../client"
}
},
"final": {
"loopback#urlNotFound": {}
},
"final:after": {
"loopback#errorHandler": {}
}
}
Each top-level property in middleware.json corresponds to one of the following middleware phases:
initial - The first point at which middleware can run.session - Prepare the session object.auth - Handle authentication and authorization.parse - Parse the request body.routes - HTTP routes implementing your application logic.
Middleware registered via the Express API app.use, app.route, app.get (and other HTTP verbs) runs at the beginning of this phase.
Use this phase also for sub-apps like loopback/server/middleware/rest or loopback-explorer.
files - Serve static assets (requests hit the file system here).
final - Deal with errors and requests for unknown URLs.Each phase has "before" and "after" subphases in addition to the main phase, encoded following the phase name, separated by a colon. For example, for the "initial" phase, middleware executes in this order:
initial:before initialinitial:afterMiddleware within a single subphase executes in the order in which it is registered. However, you should not rely on such order. Always explicitly order the middleware using appropriate phases when order matters.
In general, each phase has the following syntax:
phase[:sub-phase] : {
middlewarePath : {
[ enabled: [true | false] ]
[, name: nameString ]
[, params : paramSpec ]
[, methods: methodSpec ]
[, paths : routeSpec ]
}
}
Where:
before or after."methods" : ["GET", "POST"].
If not present, applies to all methods.For more information, see Defining middleware.
Set Cross-origin resource sharing (CORS) settings as cors.params properties in the initial phase.
You can set other CORS properties as well. For more information, see cors.
| Property | Type | Description | Default |
|---|---|---|---|
| cors.params.origin | Boolean |
Configures the Access-Control-Allow-Origin CORS header.
Expects a string (for example: `http://example.com/`).
Set to |
true |
| cors.params.credentials | Boolean |
Configures the Access-Control-Allow-Credentials CORS header. Set to You can set other cors properties as well. For more information, see cors. |
true |
| cors.params.maxAge | Number | Configures the Access-Control-Allow-Max-Age CORS header. Set to an integer to pass the header, otherwise it is omitted. | 86400 |