'JSON server and custom routes
I'm using JSON server. If I initialize it with npx json-server --watch database.json --port 5000
and then open http://localhost:5000/items
I can see all the items. If I go to http://localhost:5000/items/1
I have only one item. My question is - how can I change single item route so I get the first item by entering http://localhost:5000/items/myCustomId1
? In other words, how can I replace id
with customId
?
{
"items": [
{
"id": 1,
"customId": "myCustomId1",
"name": "Item 1"
},
{
"id": 2,
"customId": "myCustomId2",
"name": "Item 2"
}
]
}
Solution 1:[1]
I think the best you can do here is using parameters handling for that request. For instance, you can do http://localhost:5000/items?myCustomId=1
in your case. This will probably list all the items with customId of 1 instead of just 1 item (since customId is not unique in this case).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Minh Luu |