I'm trying to get Json-Server (v 0.12.1) working without the need for middleware or using it as a component - I already have something that works fine if I'm going to take the time to manually code all the endpoints. I'm attracted to the simplistic route config, and I believe I should be able to use it without requiring anything but a routes.json
and db.json
(letting me remove a lot of boilerplate express stuff).
My db.json:
"users": [
{
"userId": 1,
"favoriteColor": "red"
},
{
"userId": 2,
"favoriteColor": "blue"
},
{
"userId": 3,
"favoriteColor": "green"
}
]
My routes.json:
{
"/api/users": "/users",
"/api/users/:userId": "/users/:userId"
}
I want to be able to make a GET call to: http://localhost:3000/api/users/2
and get
{
"userId": 2,
"favoriteColor": "blue"
}
in return. However, I always just get {}
.
Trying to use some suggestions from SO which either change the data structure (which I don't have control over) or using filters (which returns an array, not an object) are no-go for me.
Does anyone know why this seemingly-simply route is not working? I believe I'm following the custom route guidelines. I'm starting the server with json-server --watch ./api/db.json --routes ./api/routes.json
, and hitting http://localhost:3000/api/users
returns exactly what I expect, so I know the routes.json
file and db.json
file are both being picked up successfully.
Thank you!
I gotta working starting the server with:
json-server --watch db.json --routes routes.json
db.json
{
"users": [
{
"id": 1,
"first_name": "Sebastian",
"last_name": "Eschweiler",
"email": "sebastian@codingthesmartway.com"
},
{
"id": 2,
"first_name": "Steve",
"last_name": "Palmer",
"email": "steve@codingthesmartway.com"
},
{
"id": 3,
"first_name": "Ann",
"last_name": "Smith",
"email": "ann@codingthesmartway.com"
}
]
}
routes.json
{
"/api/users": "/users",
"/api/users/:id": "/users/:id"
}