Tuesday 17 April 2018

How do I construct resource URLs on REST API server?

Suppose I have a REST service with controller methods like this (written using a helper for expressJS):@route('/api/v1/users/:id') getUser(id) { return json({ user: getUserFromDatabase(id), friendsUrl: '/api/v1/users/' + id + '/friends' }); } As you can see, the creation of friendsUrl link is hardcoded here.I'm pretty sure this is a bad idea, because my app is large, and I may decide to change my urls, my api version, or something else.What is the state-of-the-art way to construct REST resource URLs on the server? I suppose they should be tied to controller methods, but I'm not sure how to best do it in JS.Any advice is appreciated!

Submitted April 17, 2018 at 10:58PM by smthamazing

No comments:

Post a Comment