Created
June 4, 2013 12:51
-
-
Save morkeleb/5705647 to your computer and use it in GitHub Desktop.
Self documenting api in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
All comments are returned in the doc as documentation for the endpoint.