Last active
August 29, 2015 14:03
-
-
Save ruffrey/faa727c75a6ffafb9aaf to your computer and use it in GitHub Desktop.
Generating documentation from Node Express routes
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
function listRoutes(req, res, next) { | |
var routes = 'get post patch put delete', // which methods do you want to inspect? | |
ordered = [], | |
Controller = new require('../classes/Controller'), | |
tempController = new Controller("Account"), | |
No = tempController.no.toString(), | |
Plural = tempController.plural.toString(), | |
reString = config.odm.models.map( | |
function(m){ | |
return "(" + m + ")"; | |
} | |
).join('|'), | |
reModelList = new RegExp(reString, 'g'); | |
for(var m in req.app.routes) | |
{ | |
if( | |
routes.indexOf(m.toLowerCase()) != -1 | |
) | |
{ | |
for(var r=0; r<req.app.routes[m].length; r++) | |
{ | |
var resrc = req.app.routes[m][r].path.split('/')[1]; | |
if(!resrc) resrc = "/"; | |
if( req.app.routes[m][r].path != '*' | |
// only show plural route paths, and UI, plus the special ones | |
&& ( | |
req.app.routes[m][r].callbacks[0].toString() != Plural | |
&& resrc.lastIndexOf('*') != (resrc.length-1) | |
&& resrc != "ui" | |
) | |
) | |
{ | |
// Express has the functions as ".callbacks" property. | |
var cbs = req.app.routes[m][r] | |
.callbacks | |
.join(' '); | |
var notallowed = req.app.routes[m][r].callbacks[0].toString() == No; | |
// Checks for stream comments and line comments. | |
var commentArray = cbs.match( | |
/(\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\/)|(\/\/.*)/g | |
); | |
var comments = ""; | |
// When there are comments, replace the deadspace with something | |
// that will render better as html. | |
if(commentArray && commentArray.length) | |
{ | |
comments = commentArray | |
.join("<br />") | |
.replace(/\/\//g, '') | |
.replace(/\/\*+/g, '') | |
.replace(/\*+\//g, '') | |
.replace(/\t/g, ' '); | |
} | |
/* jshint ignore:start */ | |
var returnString = comments.match(/\@returns\s(.+)\<br \/\>/g); | |
returnString = returnString && returnString.length ? returnString[0].split('<br />')[0].replace('@returns ','') : ""; | |
// Make it pretty | |
comments = comments | |
.replace(reModelList, '<a href="/ui/models#$&" target="_blank">$&</a>') | |
.replace(/(\@)([a-z]+\s)/g, '<strong><em>$2</em></strong>'); | |
/* jshint ignore:end */ | |
var obj = { | |
method: m.toUpperCase(), | |
path: req.app.routes[m][r].path, | |
cb: cbs, | |
resource: resrc, | |
comments: comments, | |
notallowed: notallowed | |
}; | |
if(returnString) obj.returnString = returnString; | |
ordered.push(obj); | |
} | |
} | |
} | |
} | |
ordered.sort(function(a, b){ | |
if (a.path < b.path) return -1; | |
if (a.path > b.path) return 1; | |
return 0; | |
}); | |
res.render('routeList', { | |
title: 'Available Routes', | |
routes: ordered | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment