Created
August 14, 2013 23:10
-
-
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"
This file contains hidden or 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
| 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); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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".