I am attempting to retrieve an object by its id using json-server. If I only return a single variable (one JSON) in the module.exports of the database file, everything works fine. However, if I try to return an object with multiple variables, I encounter a 404 error.
The URLs for resources are: http://localhost:3000/users http://localhost:3000/admins
GET users returns a status code of 200 in both scenarios. GET admins also returns a status code of 200 in both scenarios. However, when trying to GET users/1234 while returning multiple JSON files, it results in a 404.
The content of the database file db.js:
var users = require('./users.json');
var admins = require('./admins.json');
module.exports = function() {
return {
users : users,
admins : admins
}; // When returning like this, 'GET users' and 'GET admins' work, but 'GET users/1234' fails
// return users; // When returning just 'users', all three requests work - 'GET users', 'GET admins', and 'GET users/1234'
}
The contents of the JSON file users.json:
{
"users": [
{
"id": 1234,
"name": "Andrew Owen",
"age": 26,
"eyeColor": "blue"
},
{
"id": 1235,
"name": "Susan prueba",
"age": 45,
"eyeColor": "hazel"
}
]
}
Command used to run: json-server --watch db.js