strapi/docs/3.0.0-beta.x/concepts/middlewares.md

189 lines
4.3 KiB
Markdown
Raw Normal View History

# 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
2020-04-01 11:48:38 +02:00
### File structure
```js
module.exports = strapi => {
return {
2019-06-08 18:50:07 +02:00
// can also be async
initialize() {
strapi.app.use(async (ctx, next) => {
2020-04-01 11:48:38 +02:00
// await someAsyncCode()
await next();
2020-04-01 11:48:38 +02:00
// await someAsyncCode()
});
},
};
};
```
2019-06-08 18:50:07 +02:00
- `initialize` (function): Called during the server boot.
2020-04-01 11:48:38 +02:00
The middlewares are accessible through the `strapi.middleware` variable.
2020-04-01 11:48:38 +02:00
### Node modules
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.
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
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
│ └─── responseTime // It will override the core default responseTime middleware.
│ - index.js
│ └─── views // It will be added into the stack of middleware.
│ - 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
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.
```javascript
{
...
"middleware-name": {
"enabled": true,
...
}
}
```
2020-04-01 11:48:38 +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
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.
2020-04-01 11:48:38 +02:00
### Examples
2020-04-01 11:48:38 +02:00
Create your custom middleware.
2020-04-01 11:48:38 +02:00
**Path —** `./middlewares/timer/index.js`
2020-04-01 11:48:38 +02:00
```js
module.exports = strapi => {
return {
initialize() {
strapi.app.use(async (ctx, next) => {
const start = Date.now();
2020-04-01 11:48:38 +02:00
await next();
2020-04-01 11:48:38 +02:00
const delta = Math.ceil(Date.now() - start);
2020-04-01 11:48:38 +02:00
ctx.set('X-Response-Time', delta + 'ms');
});
},
};
};
```
2020-04-01 11:48:38 +02:00
Enable the middleware in environments settings.
**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
**Path —** `./config/middleware.json`
```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
],
"after": ["parser", "router"]
}
}
```