node.js - sailsjs routes for both generated and defined -
node.js - sailsjs routes for both generated and defined -
i'm trying finish list of api methods available service. show api calls we're exposing through controllers. know can introspect sails.config.routes object list of defined routes. there doesn't seem easy way list of design routes "automatically" generated you. 1 thought assume specific pattern 3 different types of design generates (action, rest, , shortcuts). bad thought simple fact framework in it's infancy , things subject change. i'd rather rely on method list based on defined, , if alter code should reflect automatically.
so dug sails , under covers sails rely's on express routing.
in sails source initialize.js on line 35 found bit of code.
// create express server var app = sails.hooks.http.app = express();
so searched on how others have output api's using express. brought me different pages. http://thejackalofjavascript.com/list-all-rest-endpoints/ , how registered routes in express?, both of them helpful. made me realize sails using 3.4.3 version of express , not 4. rather disappointing considering express 4 has been out year now. ok let's getto point of problem. stored in sails.hooks.http.app.routes there lot of duplication in routes , kinda of messy output itself. here how able output in nicer way.
//where routes 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 helpful else looking accomplish similar effect. think it's kinda sad type of info isn't exposed in nicer way. hidden us, , have hunt , peck through dev's code. goal have service phone call expose angularjs service consumes api , reflect methods available. making service much easier consume , removing need sort of mapping manually.
enjoy!
node.js sails.js
Comments
Post a Comment