Skip to content

Instantly share code, notes, and snippets.

@ericsaboia
Created August 14, 2013 23:10
Show Gist options
  • Save ericsaboia/6236664 to your computer and use it in GitHub Desktop.
Save ericsaboia/6236664 to your computer and use it in GitHub Desktop.
List routes for node restify or express, inspired by rake routes (Rails). Usage: Just put inside your routes folder, and run "node list_routes.js"
var fs = require('fs');
fs.readdir(__dirname, function(err, files) {
sayRoutes(files);
});
function sayRoutes (files) {
files.forEach(function (file) {
read(file, function (routes) {
if (routes.length > 0) {
console.log(file.replace('.js', ''));
routes.forEach(function (route) {
console.log(" " +route.type + " " + route.request);
});
console.log();
}
});
});
}
function read(file, callback) {
fs.readFile(__dirname + "/" + file, 'UTF-8', function (err, str) {
searchRoutes(str, function (routes) {
callback(routes);
});
});
}
function searchRoutes (str, callback) {
var regex = /server\.(put|get|post|del|head)\(['"](.+)['"],/i;
var lines = str.split("\n");
var routes = [];
lines.forEach(function (line) {
var match = regex.exec(line);
if (match) {
routes.push({type: match[1].toUpperCase(), request: match[2]});
}
});
callback(routes);
}
@fabiancarlos
Copy link

Just a note, if anyone have a blank result, check the regex on searchRoutes function, on this file is "server" and my i use "app".

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