插件的服务端 API:概览
页面摘要: 服务端 API 定义了插件在 Strapi 服务端注册、暴露和执行的内容。它涵盖生命周期 hook、路由、控制器、服务、策略、中间件和配置。使用入口文件声明插件贡献的内容,然后跳转到下面的各个专用页面以了解每种能力。
一个 Strapi 插件可以同时与 Strapi 应用的后端和前端交互。服务端 API 涵盖后端部分:它定义了插件在 Strapi 服务端注册、暴露和执行的内容。服务端部分在入口文件中定义,该文件导出一个对象(或返回对象的函数)。该对象描述了插件对服务端的贡献。
有关插件如何自定义管理面板 UI 的更多信息,请参阅 管理面板 API。
WARNING
在深入阅读本页概念之前,请确保你已经创建了一个 Strapi 插件。
入口文件
服务端 API 的入口文件是 [plugin-name]/server/src/index.js|ts。该文件导出所需的接口,其中包含以下可用参数:
| Parameter type | Available parameters |
|---|---|
| Lifecycle functions | register(), bootstrap(), destroy() |
| Configuration | config 对象 |
| Backend customizations | contentTypes, routes, controllers, services, policies, middlewares |
一个最小的入口文件如下所示:
JavaScript
'use strict';
const register = require('./register');
const bootstrap = require('./bootstrap');
const destroy = require('./destroy');
const config = require('./config');
const contentTypes = require('./content-types');
const routes = require('./routes');
const controllers = require('./controllers');
const services = require('./services');
const policies = require('./policies');
const middlewares = require('./middlewares');
module.exports = () => ({
register,
bootstrap,
destroy,
config,
contentTypes,
routes,
controllers,
services,
policies,
middlewares,
});
TypeScript
import register from './register';
import bootstrap from './bootstrap';
import destroy from './destroy';
import config from './config';
import contentTypes from './content-types';
import routes from './routes';
import controllers from './controllers';
import services from './services';
import policies from './policies';
import middlewares from './middlewares';
export default () => ({
register,
bootstrap,
destroy,
config,
contentTypes,
routes,
controllers,
services,
policies,
middlewares,
});
从技术上讲,所有服务端代码都可以放在单个入口文件中,但强烈建议按照 Plugin SDK 生成的结构,将每个关注点拆分到各自的文件夹中。本文档中的示例遵循该结构。
说明
- 入口文件既可以接受对象字面量,也可以接受返回相同对象形态的函数。当使用函数形式时,Strapi 在加载插件模块时会以
{ env }(而不是{ strapi })调用它。 config是一个配置对象,而不是可执行的 lifecycle hook。与register()、bootstrap()或destroy()不同,它在插件生命周期中不会作为函数被调用。它在启动时加载,用于设置默认值并验证用户配置。详见 服务端生命周期。
可用操作
服务端 API 让插件利用多个构建块来定义其服务端行为。
使用下表查找哪种能力与你的目标匹配:
| Goal | Parameter to use | When it runs |
|---|---|---|
| Run code before the server starts | register() | 在数据库和路由初始化之前 |
| Run code after all plugins are loaded | bootstrap() | 在数据库、路由和权限初始化之后 |
| Clean up resources on shutdown | destroy() | 在关闭时 |
| Define plugin options with defaults and validation | config | 在启动时加载 |
| Declare plugin content-types | contentTypes | 在启动时加载 |
| Expose HTTP endpoints | routes | 在启动时加载 |
| Handle HTTP requests | controllers | 每次请求时调用 |
| Implement business logic | services | 从控制器或生命周期 hook 调用 |
| Enforce access rules on routes | policies | 每次请求时评估,在控制器之前 |
| Intercept and modify request/response flow | middlewares | 在 register() 中附加或在路由配置中引用 |
| Access plugin features at runtime | Getters | 任何生命周期或请求处理程序 |
以下卡片直接链接到各个专用页面:
- Lifecycle — Control when plugin server logic runs with register, bootstrap, and destroy hooks.
- Configuration — Declare default plugin options and validate user-provided config from config/plugins.
- Content-types — Declare plugin content-types and access them at runtime through the Document Service API.
- Routes — Expose plugin endpoints as Content API or admin routes with full control over auth and policies.
- Controllers & services — Handle requests in controllers and organize reusable business logic in services.
- Policies & middlewares — Enforce access rules with policies and intercept request flow with middlewares.
- Getters & usage — Access plugin controllers, services, content-types, and config through top-level and global getters.
- Extending the MCP server — Register custom MCP tools through the strapi.ai.mcp service so AI clients can trigger plugin-specific actions.