The middlewares are functions which are composed and executed in a stack-like manner upon request. If you are not familiar with the middleware stack in Koa, we highly recommend you to read the [Koa's documentation introduction](http://koajs.com/#introduction).
-`initialize` (function): Called during the server boot. The callback `cb` needs to be called. Otherwise, the middleware won't be loaded into the stack.
The framework allows the application to override the default middlewares and add new ones. You have to create a `./middlewares` folder at the root of your project and put the middlewares into it.
```
/project
└─── admin
└─── api
└─── config
└─── middlewares
│ └─── responseTime // It will override the core default responseTime middleware
│ └─── views // It will be added into the stack of middleware
The middlewares are injected into the Koa stack asynchronously. Sometimes it happens that some of these middlewares need to be loaded in a specific order. To define a load order, we created a dedicated file located in `./config/middleware.json`.
**Path —** `./config/middleware.json`.
```json
{
"timeout": 100,
"load": {
"before": [
"responseTime",
"logger",
"cors",
"responses"
],
"order": [
"Define the middlewares' load order by putting their name in this array in the right order"
],
"after": [
"parser",
"router"
]
}
}
```
-`timeout`: defines the maximum allowed milliseconds to load a middleware.
-`load`:
-`before`: array of middlewares that need to be loaded in the first place. The order of this array matters.
-`order`: array of middlewares that need to be loaded in a specific order.
-`after`: array of middlewares that need to be loaded at the end of the stack. The order of this array matters.
The `responseTime` middleware will be loaded first. Immediately followed by the `logger` middleware. Then, the others middlewares will be loaded asynchronously.
The `router` middleware will be loaded at the very end. The `parser` middleware will be loaded after all the others and just before the `router` middleware.