I’ve seen examples of Restify where all the endpoints are located on the root: /users, /data, etc. I know it’s possible to implement nesting like so:
server.get('/users/:user/data/:id', returnData);
and the req.params variable will have all of the request parameters. Example:
{ user: '45', id: '80' }
This seems to work fine if my application has few endpoints, but what if I have a deep and branched data structure that I want to expose through a REST API? Something like:
{
stuff: {
points: {
colors: {
shinyThings: {},
dullThings: {}
}
},
ships: {
enterprises: {},
starDestroyers: {}
}
},
things: {},
}
Having to write the paths to all of these endpoints by hand just doesn’t seem right. I end up with lots of path definitions and stuff like this:
server.put('/stuff/:stuff/points/:points/colors/:colors/shinyThings/:shinyThings', returnShinyThing);
Is there an easier way to do this with Restify?
I’ve come up with a way to do it, although I’m sure there are better alternatives:
1) Create modules to handle certain actions on endpoints. These modules will be required into a central router module. Example
stuff.js:2) In the router module define a mapping of actions -> http verbs:
3) Create an object representing the data structure like this:
4) During the router initialization the application passes it a Restify server object to attach the routes to. During initialization a recursive function walks the schema object and when a
_actionskey is found it calls a second function that attaches the route handlers at the given path to the given server object:Notes: This makes use of the lingo module (i.e. the en.singularize() function). It’s also a bit simplified since I removed non-critical parts of the functions, but it should be fully functional as it is.
The inspiration for this came after looking at the way express-resource does it, although it’s not as refined and simple to use.