路由
¥Routes
通过任何 URL 发送到 Strapi 的请求均由路由处理。默认情况下,Strapi 为所有内容类型生成路由(参见 REST API 文档)。路由可以是 added 并配置:
¥Requests sent to Strapi on any URL are handled by routes. By default, Strapi generates routes for all the content-types (see REST API documentation). Routes can be added and configured:
-
使用 policies,这是一种阻止访问路由的方法,
¥with policies, which are a way to block access to a route,
-
以及 middlewares,这是一种控制和更改请求流和请求本身的方法。
¥and with middlewares, which are a way to control and change the request flow and the request itself.
一旦存在一条路由,到达该路由就会执行一些由控制器处理的代码(参见 控制器文档)。要查看所有现有路线及其层次顺序,你可以运行 yarn strapi routes:list
(参见 CLI 参考)。
¥Once a route exists, reaching it executes some code handled by a controller (see controllers documentation). To view all existing routes and their hierarchal order, you can run yarn strapi routes:list
(see CLI reference).

执行
¥Implementation
实现新路由包括在 ./src/api/[apiName]/routes
文件夹内的路由文件中定义它(请参阅 项目结构)。
¥Implementing a new route consists in defining it in a router file within the ./src/api/[apiName]/routes
folder (see project structure).
根据用例,有 2 种不同的路由文件结构:
¥There are 2 different router file structures, depending on the use case:
-
配置 核心路由
¥configuring core routers
-
或创建 定制路由。
¥or creating custom routers.
配置核心路由
¥Configuring core routers
核心路由(即 find
、findOne
、create
、update
和 delete
)对应于创建新 content-type 时由 Strapi 自动创建的 默认路由。
¥Core routers (i.e. find
, findOne
, create
, update
, and delete
) correspond to default routes automatically created by Strapi when a new content-type is created.
Strapi 提供了 createCoreRouter
工厂功能,可自动生成核心路由并允许:
¥Strapi provides a createCoreRouter
factory function that automatically generates the core routers and allows:
-
将配置选项传递给每个路由
¥passing in configuration options to each router
-
并禁用一些核心路由到 创建自定义的。
¥and disabling some core routers to create custom ones.
核心路由文件是一个 JavaScript 文件,使用以下参数导出对 createCoreRouter
的调用结果:
¥A core router file is a JavaScript file exporting the result of a call to createCoreRouter
with the following parameters:
范围 | 描述 | 类型 | |
---|---|---|---|
prefix | 允许传入自定义前缀以添加到该型号的所有路由(例如 /test ) | String | |
only | 只会加载的核心路由 任何不在此数组中的内容都会被忽略。 | Array | --> |
except | 不应加载的核心路由 这在功能上与 only 参数相反。 | Array | |
config | 配置处理路由的 policies、middlewares 和 公开可用性 | Object |
- JavaScript
- TypeScript
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::restaurant.restaurant', {
prefix: '',
only: ['find', 'findOne'],
except: [],
config: {
find: {
auth: false,
policies: [],
middlewares: [],
},
findOne: {},
create: {},
update: {},
delete: {},
},
});
import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::restaurant.restaurant', {
prefix: '',
only: ['find', 'findOne'],
except: [],
config: {
find: {
auth: false,
policies: [],
middlewares: [],
},
findOne: {},
create: {},
update: {},
delete: {},
},
});
通用实现示例:
¥Generic implementation example:
- JavaScript
- TypeScript
const { createCoreRouter } = require('@strapi/strapi').factories;
module.exports = createCoreRouter('api::restaurant.restaurant', {
only: ['find'],
config: {
find: {
auth: false,
policies: [],
middlewares: [],
}
}
});
import { factories } from '@strapi/strapi';
export default factories.createCoreRouter('api::restaurant.restaurant', {
only: ['find'],
config: {
find: {
auth: false,
policies: [],
middlewares: [],
}
}
});
这仅允许来自核心 find
controller 的 /restaurants
路径上的 GET
请求,无需身份验证。
¥This only allows a GET
request on the /restaurants
path from the core find
controller without authentication.