【问题标题】:API endpoints with json-server带有 json-server 的 API 端点
【发布时间】:2015-12-04 07:19:33
【问题描述】:

我正在使用 json-server 创建一个测试 API。我想做 API 端点:http://localhost:3000/posts/1/comments

/posts 和 /posts/1 工作正常,但我不确定为什么 /posts/1/cmets 不能。我错过了什么吗?

这是json

{
  "posts": [
    {
      "id": 1,
      "title": "Post 1",
      "comments": [
        {
          "id": 1,
          "body": "This is comment 1"
        }
      ]
    },
    {
      "id": 2,
      "title": "Post 2",
      "comments": []
    }
  ]
}

【问题讨论】:

    标签: json node.js api server


    【解决方案1】:

    cmets是postId引用的独立对象,如下:

    {
      "posts": [
        {
          "id": 1,
          "title": "json-server",
          "author": "typicode"
        }
      ],
      "comments": [
        {
          "id": 1,
          "body": "some comment",
          "postId": 1
        },
        {
          "id": 2,
          "body": "this is another",
          "postId": 1
        }
      ],
      "profile": {
        "name": "typicode"
      }
    }
    

    所以你可以这样做:

    本地主机/posts/1

    {
        id: 1,
        title: "json-server",
        author: "typicode"
    }
    

    localhost/posts/1/cmets

    [
        {
            id: 1,
            body: "some comment",
            postId: 1
        },
        {
            id: 2,
            body: "this is another",
            postId: 1
        }
    ]
    

    http://jsonplaceholder.typicode.com/comments

    【讨论】:

      猜你喜欢
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-09
      • 2018-05-09
      • 1970-01-01
      • 2023-02-23
      • 2023-03-12
      相关资源
      最近更新 更多