使用json-server

时间:2015-12-03 23:03:39

标签: json node.js api server

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

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

这是json

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

1 个答案:

答案 0 :(得分:1)

注释是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"
  }
}

所以你可以这样做:

  

本地主机/帖/ 1

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

本地主机/帖/ 1 /注释

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

http://jsonplaceholder.typicode.com/comments