中间件配置
¥Middlewares configuration
在 Strapi 中,3 个中间件概念共存:
¥In Strapi, 3 middleware concepts coexist:
-
整个 Strapi 服务器应用的全局中间件是 配置并启用。这些中间件可以应用于应用级别或 API 级别。
当前文档描述了如何实现它们。
插件还可以添加全局中间件(参见 服务器 API 文档)。¥Global middlewares are configured and enabled for the entire Strapi server application. These middlewares can be applied at the application level or at the API level.
The present documentation describes how to implement them.
Plugins can also add global middlewares (see Server API documentation). -
路由中间件的范围更有限,并且在路由级别被配置和用作中间件。它们在 路由文档 中进行了描述。
¥Route middlewares have a more limited scope and are configured and used as middlewares at the route level. They are described in the routes documentation.
-
文档服务中间件适用于文档服务 API,并具有自己的 implementation 和相关的 生命周期钩子。
¥Document Service middlewares apply to the Document Service API and have their own implementation and related lifecycle hooks.
./config/middlewares.js
文件用于定义 Strapi 服务器应应用的所有全局中间件。
¥The ./config/middlewares.js
file is used to define all the global middlewares that should be applied by the Strapi server.
仅应用 ./config/middlewares.js
中存在的中间件。加载中间件发生在特定的 加载订单 中,每个中间件都有一些 命名约定 和 可选配置。
¥Only the middlewares present in ./config/middlewares.js
are applied. Loading middlewares happens in a specific loading order, with some naming conventions and an optional configuration for each middleware.
Strapi 使用内置的内部中间件预填充 ./config/middlewares.js
文件,这些中间件都有自己的 配置选项。
¥Strapi pre-populates the ./config/middlewares.js
file with built-in, internal middlewares that all have their own configuration options.
加载顺序
¥Loading order
./config/middlewares.js
文件导出一个数组,其中顺序很重要,并控制中间件堆栈的执行顺序:
¥The ./config/middlewares.js
file exports an array, where order matters and controls the execution order of the middleware stack:
- JavaScript
- TypeScript
module.exports = [
// The array is pre-populated with internal, built-in middlewares, prefixed by `strapi::`
'strapi::logger',
'strapi::errors',
'strapi::security',
'strapi::cors',
// custom middleware that does not require any configuration
'global::my-custom-node-module',
// custom name to find a package or a path
{
name: 'my-custom-node-module',
config: {
foo: 'bar',
},
},
// custom resolve to find a package or a path
{
resolve: '../some-dir/custom-middleware',
config: {
foo: 'bar',
},
},
// custom configuration for internal middleware
{
name: 'strapi::poweredBy',
config: {
poweredBy: 'Some awesome company',
},
},
// remaining internal & built-in middlewares
'strapi::query',
'strapi::body',
'strapi::session',
'strapi::favicon',
'strapi::public',
];
export default [
// The array is pre-populated with internal, built-in middlewares, prefixed by `strapi::`
'strapi::logger',
'strapi::cors',
'strapi::body',
'strapi::errors',
// ...
'my-custom-node-module', // custom middleware that does not require any configuration
{
// custom name to find a package or a path
name: 'my-custom-node-module',
config: {
foo: 'bar',
},
},
{
// custom resolve to find a package or a path
resolve: '../some-dir/custom-middleware',
config: {
foo: 'bar',
},
},
];
如果你不确定将中间件放置在堆栈中的位置,请将其添加到列表的末尾。
¥If you aren't sure where to place a middleware in the stack, add it to the end of the list.
命名约定
¥Naming conventions
全局中间件根据其来源可以分为不同类型,定义了以下命名约定:
¥Global middlewares can be classified into different types depending on their origin, which defines the following naming conventions:
中间件类型 | 起源 | 命名约定 |
---|---|---|
内部的 | 内置中间件(即包含在 Strapi 中),自动加载 | strapi::middleware-name |
应用层 | 从 ./src/middlewares 文件夹加载 | global::middleware-name |
API 级 | 从 ./src/api/[api-name]/middlewares 文件夹加载 | api::api-name.middleware-name |
插入 | 从 strapi-server.js 导出到 插件接口的 middlewares 属性 | plugin::plugin-name.middleware-name |
外部的 | 可以是:
| - 由于它们是直接从配置文件中配置和解析的,因此没有命名约定。 |
可选配置
¥Optional configuration
中间件可以有一个带有以下参数的可选配置:
¥Middlewares can have an optional configuration with the following parameters:
范围 | 描述 | 类型 |
---|---|---|
config | 用于定义或覆盖中间件配置 | Object |
resolve | 中间件文件夹的路径(对于外部中间件有用) | String |