开发者

parse express app.js route

How to ge开发者_StackOverflow社区t a list of all available routes based on the all.js file.?


You can dig around inside the app.routes object, which is an instance of Router. The easiest way would probably be to load up your app in an interactive listener and just take a look at the various objects contained in app.routes.

If you want to be more methodical, you can read the source for the router and route objects, e.g. https://github.com/visionmedia/express/blob/master/lib/router/route.js


There's actually a better way of doing this, TJ Holowaychuck (the author of Express) made a gist with it:

app.routes.all().forEach(function(route){
  console.log('  \033[90m%s \033[36m%s\033[0m', route.method.toUpperCase(), route.path);
});


for(var type in app.routes.routes) {
  console.log(type+":");
  for(var rts in app.routes.routes[type]) {
    console.log(app.routes.routes[type][rts]);
  }
}


The routes object has verbs as properties. You can iterate over them and construct a list of all routes, methods etc.

I created a Gist creating a self describing rest-api. I'm sure it can be improved but if you want you can see how I iterate over the routes object there.

https://gist.github.com/morkeleb/5705647

Feedback is welcome on the Gist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜