Skip to content

Instantly share code, notes, and snippets.

@morkeleb
Created June 4, 2013 12:51
Show Gist options
  • Save morkeleb/5705647 to your computer and use it in GitHub Desktop.
Save morkeleb/5705647 to your computer and use it in GitHub Desktop.
Self documenting api in node.js
app.get('/api/', function(req, res) {
// This endpoint
//returns the routes available in the api
var routes = [];
for(var verb in app.routes){
app.routes[verb].forEach(function(route) {
var doc = route.callbacks[route.callbacks.length-1].toString().split('\n').filter(function(str) {
var doc = str.match(/(?:\s\/\/)(.*)/);
if(doc == null) return false;
return true;
}).map(function(doc) {
return doc.match(/(?:\s\/\/)(.*)/)[1];
}).join('\n');
routes.push({method: verb, path: route.path, keys: route.keys, doc: doc});
});
}
res.json(routes);
});
@morkeleb
Copy link
Author

morkeleb commented Jun 4, 2013

All comments are returned in the doc as documentation for the endpoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment