如何在 Strapi 中添加新的 API 端点路由?

ayc*_*ent 1 strapi

我是 Strapi 和后端的新手。

我创建了一个名为 post 的内容类型/posts/:id,默认情况下可以通过此路由访问它。但是,我也希望在这条路线上可以访问这篇文章/posts/:slug。我尝试在routes.json文件中添加一条新路由,但是当我访问该路由时,它返回 404 错误。

我如何实现这一目标?

编辑:

我的routes.json文件看起来像这样:

{
  "routes": [{
      "method": "GET",
      "path": "/posts",
      "handler": "Post.find",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/posts/count",
      "handler": "Post.count",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/posts/:id",
      "handler": "Post.findOne",
      "config": {
        "policies": []
      }
    },
    {
      "method": "GET",
      "path": "/posts/:slug",
      "handler": "Post.findOne",
      "config": {
        "policies": []
      }
    },
    {
      "method": "POST",
      "path": "/posts",
      "handler": "Post.create",
      "config": {
        "policies": []
      }
    },
    {
      "method": "PUT",
      "path": "/posts/:id",
      "handler": "Post.update",
      "config": {
        "policies": []
      }
    },
    {
      "method": "DELETE",
      "path": "/posts/:id",
      "handler": "Post.delete",
      "config": {
        "policies": []
      }
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

nop*_*rt1 7

问题是您的新slug端点使用的处理程序与通过其唯一标识符(给每个帖子的递增编号)查找帖子的处理程序相同。所以它没有任何线索,perfect-simplicity因为它期待一个数值。

为了解决您的问题,您需要为您的端点创建一个新的处理程序/控制器。

这是 Strapi 文档的链接:https ://strapi.io/documentation/3.0.0-alpha.x/guides/controllers.html#adding-endpoints

您在创建路线时是正确的:

小路 - ./api/hello/config/routes.json

{
  "routes": [
    {
      "method": "GET",
      "path": "/hello",
      "handler": "Hello.index"
    }
  ]
}
Run Code Online (Sandbox Code Playgroud)

只是您的处理程序需要更改。

小路 - ./api/hello/controllers/Hello.js

module.exports = {
  // GET /hello
  index: async ctx => {
    ctx.send('Hello World!');
  },
};
Run Code Online (Sandbox Code Playgroud)

正如文档中提到的那样,请注意:

路由处理程序只能访问./api/**/controllers文件夹中定义的控制器。


除此之外,您真的不应该使用,slug因为我认为它不是您帖子中的唯一标识符(除非您阻止用户创建具有相同perfect-simplicityslug 的帖子)。这不是最佳实践。如果您允许通过帖子的slug值访问帖子,您将来可能会遇到很多问题。


Jim*_*RIE 5

如果您需要能够找到 的文章,我建议您使用此文档slug更新控制器功能 - https://strapi.io/documentation/3.0.0-beta.x/concepts/controllers.html#核心控制器findOne

\n\n

这是自定义函数

\n\n

路径\xe2\x80\x94 ./api/article/controllers/Article.js

\n\n
const { sanitizeEntity } = require(\'strapi-utils\');\n\nmodule.exports = {\n  async findOne(ctx) {\n    //check if the params id is an id or a slug\n    const {id} = ctx.params;\n\n    // if you use MongoDB database\n    // we are validating that the id match ObjectID format\n    if (id.match(/^[0-9a-fA-F]{24}$/)) {\n      const entity = await strapi.services.article.findOne(ctx.params);\n      return sanitizeEntity(entity, { model: strapi.models.article });\n    }\n\n    // if you use SQL database\n    // we check if the id is a valid number\n    if (parseInt(id) == id) {\n      const entity = await strapi.services.article.findOne(ctx.params);\n      return sanitizeEntity(entity, { model: strapi.models.article });\n    }\n\n    // findOne function works only with IDs\n    // so we find all and get first entry by using slug\n    const [entity] = await strapi.services.article.find({slug: id});\n    return sanitizeEntity(entity, { model: strapi.models.article });\n  }\n};\n
Run Code Online (Sandbox Code Playgroud)\n