Skip to main content

后端定制

¥Back-end customization

🤓 消歧义:带子后端

作为无头 CMS,整个 Strapi 软件可以被视为你网站或应用的 "后端"。但 Strapi 软件本身包括 2 个不同的部分:

¥As a headless CMS, the Strapi software as a whole can be considered as the "back end" of your website or application. But the Strapi software itself includes 2 different parts:

  • Strapi 的后端部分是 Strapi 运行的 HTTP 服务器。与任何 HTTP 服务器一样,Strapi 后端接收请求并发送响应。你的内容存储在数据库中,Strapi 后端与数据库交互以创建、检索、更新和删除内容。

    ¥The back-end part of Strapi is an HTTP server that Strapi runs. Like any HTTP server, the Strapi back end receives requests and send responses. Your content is stored in a database, and the Strapi back end interacts with the database to create, retrieve, update, and delete content.

  • Strapi 的前端部分称为管理面板。管理面板提供图形用户界面来帮助你构建和管理内容。

    ¥The front-end part of Strapi is called the admin panel. The admin panel presents a graphical user interface to help you structure and manage the content.

在本开发者文档中,'后端' 专指 Strapi 的后端部分。

¥Throughout this developer documentation, 'back end' refers exclusively to the back-end part of Strapi.

用户指南 解释了如何使用管理面板,管理面板自定义部分 详细介绍了管理面板可用的各种自定义选项。

¥The User Guide explains how to use the admin panel and the admin panel customization section details the various customization options available for the admin panel.

Strapi 后端运行基于后端 JavaScript 框架 相思木 的 HTTP 服务器。

¥The Strapi back end runs an HTTP server based on Koa, a back-end JavaScript framework.

与任何 HTTP 服务器一样,Strapi 后端接收请求并发送响应。你可以向 Strapi 后端发送请求,以通过 RESTGraphQL API 创建、检索、更新或删除数据。

¥Like any HTTP server, the Strapi back end receives requests and send responses. You can send requests to the Strapi back end to create, retrieve, update, or delete data through the REST or GraphQL APIs.

请求可以通过 Strapi 后端传输,如下所示:

¥A request can travel through the Strapi back end as follows:

  1. Strapi 服务器收到 request

    ¥The Strapi server receives a request.

  2. 请求命中按顺序运行的 全局中间件

    ¥The request hits global middlewares that are run in a sequential order.

  3. 请求命中 route
    默认情况下,Strapi 会为你创建的所有内容类型生成路由文件(请参阅 REST API 文档),并且可以添加和配置更多路由。

    ¥The request hits a route.
    By default, Strapi generates route files for all the content-types that you create (see REST API documentation), and more routes can be added and configured.

  4. 路由策略 充当只读验证步骤,可以阻止对路由的访问。路由中间件 可以控制请求流并在继续之前改变请求本身。

    ¥Route policies act as a read-only validation step that can block access to a route. Route middlewares can control the request flow and mutate the request itself before moving forward.

  5. 控制器 在到达路由后执行代码。服务 是可选的附加代码,可用于构建可由控制器重用的自定义逻辑。

    ¥Controllers execute code once a route has been reached. Services are optional, additional code that can be used to build custom logic reusable by controllers.

  6. 控制器和服务执行的代码与 models 交互,models 是存储在数据库中的内容数据结构的表示。
    与模型表示的数据的交互由 实体服务查询引擎 处理。

    ¥The code executed by the controllers and services interacts with the models that are a representation of the content data structure stored in the database.
    Interacting with the data represented by the models is handled by the Entity Service and Query Engine.

  7. 服务器返回 response。响应可以在发送之前通过路由中间件和全局中间件返回。

    ¥The server returns a response. The response can travel back through route middlewares and global middlewares before being sent.

全局中间件和路由中间件都包含异步回调函数 await next()。根据中间件返回的内容,请求将通过后端的较短或较长的路径:

¥Both global and route middlewares include an asynchronous callback function, await next(). Depending on what is returned by the middleware, the request will either go through a shorter or longer path through the back end:

  • 如果中间件不返回任何内容,则请求将继续穿过后端的各个核心元素(即控制器、服务以及与数据库交互的其他层)。

    ¥If a middleware returns nothing, the request will continue travelling through the various core elements of the back end (i.e., controllers, services, and the other layers that interact with the database).

  • 如果中间件在调用 await next() 之前返回,则会立即发送响应,跳过其余核心元素。然后它会沿着它出现的同一条链返回。

    ¥If a middleware returns before calling await next(), a response will be immediately sent, skipping the rest of the core elements. Then it will go back down the same chain it came up.

👀 信息

请注意,本节页面中描述的所有自定义仅适用于 REST API。GraphQL 自定义 在 GraphQL 插件文档中进行了描述。

¥Please note that all customizations described in the pages of this section are only for the REST API. GraphQL customizations are described in the GraphQL plugin documentation.

💡 通过例子学习

如果你更喜欢通过阅读示例并了解如何在实际用例中使用它们来学习,示例秘诀 部分是了解 Strapi 后端定制如何工作的另一种方式。

¥If you prefer learning by reading examples and understanding how they can be used in real-world use cases, the Examples cookbook section is another way at looking how the Strapi back end customization works.

互动图

¥Interactive diagram

下图展示了请求如何通过 Strapi 后端。你可以单击任何形状来跳转到文档中的相关页面。

¥The following diagram represents how requests travel through the Strapi back end. You can click on any shape to jump to the relevant page in the documentation.