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).
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.
Every middleware will be injected into the Koa stack. To manage the load order, please refer to the [Middleware order section](#load-order).
## Load order
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`.
The `responseTime` middleware will be loaded first. Immediately followed by the `logger` middleware. Then, the other middlewares will be loaded asynchronously.
The `gzip` middleware will be loaded after the `p3p` middleware. All the others will be loaded asynchronously.
**Load a middleware at the very end**
**Path —** `./config/middleware.json`.
```json
{
"timeout": 100,
"load": {
"before": [
...
],
"order": [],
"after": [
"parser",
"router"
]
}
}
```
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.
**Complete example**
For this example, we are going to imagine that we have 10 middlewares to load:
- cors
- cron
- favicon
- gzip
- logger
- p3p
- parser
- response
- responseTime
- router
We assume that we set the `./config/middleware.json` file like this: