2019-05-06 16:17:16 +02:00
# Middlewares
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 ).
2020-04-01 11:48:38 +02:00
## Structure
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
### File structure
2019-05-06 16:17:16 +02:00
```js
module.exports = strapi => {
return {
2019-06-08 18:50:07 +02:00
// can also be async
initialize() {
2019-05-06 16:17:16 +02:00
strapi.app.use(async (ctx, next) => {
2020-04-01 11:48:38 +02:00
// await someAsyncCode()
2019-05-06 16:17:16 +02:00
await next();
2020-04-01 11:48:38 +02:00
// await someAsyncCode()
2019-05-06 16:17:16 +02:00
});
},
};
};
```
2019-06-08 18:50:07 +02:00
- `initialize` (function): Called during the server boot.
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
The middlewares are accessible through the `strapi.middleware` variable.
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
### Node modules
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
Every folder that follows this name pattern `strapi-middleware-*` in your `./node_modules` folder will be loaded as a middleware.
2019-05-06 16:17:16 +02:00
A middleware needs to follow the structure below:
```
/middleware
└─── lib
- index.js
- LICENSE.md
- package.json
- README.md
```
The `index.js` is the entry point to your middleware. It should look like the example above.
2020-04-01 11:48:38 +02:00
### Custom middlewares
2019-05-06 16:17:16 +02:00
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
└─── api
└─── config
└─── middlewares
2020-02-08 19:37:00 +01:00
│ └─── responseTime // It will override the core default responseTime middleware.
2019-05-06 16:17:16 +02:00
│ - index.js
2020-02-08 19:37:00 +01:00
│ └─── views // It will be added into the stack of middleware.
2019-05-06 16:17:16 +02:00
│ - index.js
└─── public
- favicon.ico
- package.json
- server.js
```
Every middleware will be injected into the Koa stack. To manage the load order, please refer to the [Middleware order section ](#load-order ).
2020-04-01 11:48:38 +02:00
## Configuration and activation
2020-05-25 18:11:45 +02:00
To activate and configure your hook with custom options, you need to edit your `./config/environments/**/middleware.json` file in your Strapi app.
2020-04-01 11:48:38 +02:00
2020-04-09 11:41:55 +02:00
By default this file doesn't exist, you will have to create it.
2020-05-25 18:11:45 +02:00
```javascript
{
...
"middleware-name": {
"enabled": true,
...
}
}
```
2020-04-01 11:48:38 +02:00
2020-05-25 18:11:45 +02:00
## Core middlewares
2020-04-01 11:48:38 +02:00
The core of Strapi embraces a small list of middlewares for performances, security and great error handling.
- boom
- cors
- cron
- csp
- favicon
- gzip
- hsts
- ip
- language
- logger
- p3p
- parser
- public
- responses
- responseTime
- router
- session
- xframe
- xss
::: tip
The following middlewares cannot be disabled: responses, router, logger and boom.
:::
### Load order
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
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` .
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
**Path —** `./config/middleware.json` .
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
```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"
2019-05-06 16:17:16 +02:00
],
2020-05-25 18:11:45 +02:00
"after": ["parser", "router"]
}
}
2019-05-06 16:17:16 +02:00
```
2020-05-25 18:11:45 +02:00
- `timeout` : Defines the maximum allowed milliseconds to load a middleware.
2019-05-06 16:17:16 +02:00
- `load` :
2020-02-08 19:37:00 +01:00
- `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.
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
### Examples
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
Create your custom middleware.
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
**Path —** `./middlewares/timer/index.js`
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
```js
module.exports = strapi => {
return {
initialize() {
strapi.app.use(async (ctx, next) => {
const start = Date.now();
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
await next();
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
const delta = Math.ceil(Date.now() - start);
2019-05-06 16:17:16 +02:00
2020-04-01 11:48:38 +02:00
ctx.set('X-Response-Time', delta + 'ms');
});
},
};
};
2019-05-06 16:17:16 +02:00
```
2020-04-01 11:48:38 +02:00
Enable the middleware in environments settings.
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
**Path —** `config/environments/**/middleware.json` .
```json
{
"timer": {
"enabled": true
}
}
```
2020-04-01 11:48:38 +02:00
Load a middleware at the very first place
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
**Path —** `./config/middleware.json`
2019-05-06 16:17:16 +02:00
2020-05-25 18:11:45 +02:00
```json
{
"timeout": 100,
"load": {
"before": ["timer", "responseTime", "logger", "cors", "responses", "gzip"],
"order": [
"Define the middlewares' load order by putting their name in this array is the right order"
2020-04-01 11:48:38 +02:00
],
2020-05-25 18:11:45 +02:00
"after": ["parser", "router"]
}
}
2019-05-06 16:17:16 +02:00
```