json-server icon indicating copy to clipboard operation
json-server copied to clipboard

For multiple json files nested requests returning 404

Open riyazmulla51 opened this issue 6 years ago • 1 comments

@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)

riyazmulla51 avatar Apr 20 '20 07:04 riyazmulla51

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

valepu avatar Mar 24 '23 13:03 valepu