Skip to content

For multiple json files nested requests returning 404 #1115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
riyazmulla51 opened this issue Apr 20, 2020 · 1 comment
Open

For multiple json files nested requests returning 404 #1115

riyazmulla51 opened this issue Apr 20, 2020 · 1 comment

Comments

@riyazmulla51
Copy link

riyazmulla51 commented Apr 20, 2020

@typicode Please help me with the below problem. Thanks in adv.

//db1.json
[
{
"id": "Watson",
"userId": "Watson",
"basicDetails": {
"firstName": "Emma",
"lastName": "Watson"
},
"entitlementDetails": {
"defaultEntity": "E1",
"defaultEntityName": "United Kingdom",
"roles": [
{
"roleId": "A1",
"description": "Branch Manger"
}
],
"additionalEntities": [
{
"entityId": "E2",
"branchId": "102" }
]
}
}
]

//db2.json
[
{
"id": "001",
"partyID": "001",
"basicDetails": {
"branchCode": 99,
"addressDetails": [
{
"addressType": "3"
}
],
"contactDetails": [
{
"contactMethod": "SMS"
}
]
},
"personalDetails": {
"firstName": "ABC",
"lastName": "XYZ"
}
}
]

//to watch multiple json files i created a map.js file
//map.js

module.exports = function() {
return {
user1: require('./db1.json'),
user2: require('./db2.json')
}
}

//run json server using below command
json-server map.js

//the below one is working fine.
http://localhost:3000/user1/{userId} - Get details of the user identified by the passed user identifier

//not working
EXPECTED RESULT:
http://localhost:3000/user1/entities/{entityId} - Get details of the users belonging to a particular entity

http://localhost:3000/user1/branches/{branchId} - Get details of the users belonging to a particular branch

ACTUAL RESULT
The server responded with a status of 404 (Not Found)

@valepu
Copy link

valepu commented Mar 24, 2023

Hi,
I was trying to accomplish the same thing today, I managed to create a solution by adding prefixes to the routes of each file

db.js

const mocks = ["route1", "route2"];

module.exports = () => {
	const exp = {};
	mocks.forEach(m => {
		const db = require('./' + m + '.json');
		Object.keys(db).forEach(d => {
			exp[m + "_" + d] = db[d];
		});
	});
	
	return exp;
};

routes.json


{
  "/route1/*": "/route1_$1",
  "/route2/*": "/route2_$1"
}

and then run json-server --watch ./db.js --routes ./routes.json

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants