I have routes.json
and db.json
Route
"/api/*/_search?*=:searchstring": "/$1/?$2_like=:searchstring",
"/api/*": "/$1"
DB.json
{
"cats": {
"cats": []
},
"bats": [],
"recordList": {
"records": [
{id:1, name: 'abc'},
{id:2, name: 'def'},
{id:3, name: 'ghi'}
]
}
}
Absolutely fine fetching the record list with the above configurations.
Need to understand how to mock for the search filter call below:
http:localhost:3001/api/_search?name=abc
Updated the routes to:
{
"/api/*": "/$1",
"/api/_search?name_like": "/$1"
}
Following this 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
If 'abc' is searched, it should return
{
records: [{id: 1, name: 'abc'}]
}
You need to write your search route like this:
{
"/api/records/_search?name=:searchstring": "/records/?name_like=:searchstring"
}
Or even better, you can parametrize with *
to $1
replacement, thus you will be able to search for any parameter in query, and in any dataset, records
or other:
{
"/api/*/_search?*=:searchstring": "/$1/?$2_like=:searchstring",
"/api/*": "/$1"
}
Afterwards your request to http://localhost:3001/api/records/_search?name=ab
will be with response:
[
{
"id": 1,
"name": "abc"
}
]
Additional docs on routing.