I'm trying to get a complete list of the API methods available from my service. This is to mainly show what API calls we're exposing through all the controllers. I know that i can introspect the sails.config.routes object to get the list of defined routes. However there doesn't seem to be a easy way to get the list of blueprint routes that are "automatically" generated for you. Now one thought is that I could assume a specific pattern with the 3 different types of blueprint generates (action, rest, and shortcuts). This is a bad idea for the simple fact that this framework is in it's infancy and things are subject to change. I'd rather rely on a method to get this list based on what is truly defined, and if it does change then my code should reflect that automatically.
1 Answers
So I dug into sails and under the covers sails rely's on express for all routing.
In the sails source initialize.js on line 35 I found this bit of code.
// Create express server
var app = sails.hooks.http.app = express();
so then I searched on how others have output the api's using express. So that brought me to different pages. http://thejackalofjavascript.com/list-all-rest-endpoints/ and How to get all registered routes in Express?, both of them were very helpful. It also made me realize that sails is using 3.4.3 version of express and not 4. This is rather disappointing considering that express 4 has been out for almost a year now. Ok let's getto the point of problem. Everything is stored in sails.hooks.http.app.routes however there is a lot of duplication in the routes and is kinda of messy to output by itself. Here is how I was able to output it in a nicer way.
//where all routes are stored.
var routes = sails.hooks.http.app.routes;
var api = {};
var output = [];
for(var method in routes){
for(index in routes[method]){
var route = routes[method][index];
for(var opt in route){
if(api[method] === undefined){
api[method] = {};
}
if(api[method][route.path] == undefined && route.path !== "/*" && route.path !== "/"){
api[method][route.path] = route.params;
}
//output.push("("+typeof route + ")" + opt + " " + route[opt] + "<br/>");
}
}
}
for(method in api){
for(route in api[method]){
output.push(method + " " + route);
}
}
res.send(output.join("<br/>"));
I hope this helpful for anyone else looking to achieve a similar effect. I think it's kinda sad that this type of data isn't exposed in a nicer way. Hidden from us, and we have to hunt and peck through the dev's code. My goal will be to have a service call that will expose an angularjs service that consumes my api and will just reflect all methods available. Making this service much easier to consume and removing the need to any sort of mapping manually.
Enjoy!

- 1
- 1

- 21
- 6