使用 json-server 模拟搜索过滤器

Mock search filter using json-server

我有 routes.jsondb.json

路线

  "/api/*/_search?*=:searchstring": "//?_like=:searchstring",
  "/api/*": "/"

DB.json

 {
  "cats": {
    "cats": []
  },
  "bats": [],
  "recordList": {
    "records": [
      {id:1, name: 'abc'},
      {id:2, name: 'def'},
      {id:3, name: 'ghi'}
    ]
  }
}

完全可以通过上述配置获取记录列表。

需要了解如何模拟以下搜索过滤器调用:

http:localhost:3001/api/_search?name=abc

已将路线更新为:

{
  "/api/*": "/",
  "/api/_search?name_like": "/"
}

关注此 link:https://github.com/typicode/json-server/issues/654#issuecomment-339098881

But not hitting the config URL defined, what am I doing wrong? Am I missing something here? The search term is dynamic, hence the value passed should be acceptable from a variable only but in the comment it is static. Kindly assist with this if anyone had similar issues and resolved

如果搜索'abc',应该return

{
  records: [{id: 1, name: 'abc'}]
}

您需要这样写您的搜索路线:

{
  "/api/records/_search?name=:searchstring": "/records/?name_like=:searchstring"
}

或者更好的是,您可以使用 *</code> 替换进行参数化,因此您将能够在查询和任何数据集中搜索任何参数,<code>records 或其他:

{
  "/api/*/_search?*=:searchstring": "//?_like=:searchstring",
  "/api/*": "/"
}

之后您对 http://localhost:3001/api/records/_search?name=ab 的请求将得到响应:

[
  {
    "id": 1,
    "name": "abc"
  }
]

额外docs on routing.