Created
          October 3, 2014 13:14 
        
      - 
      
- 
        Save lukaswhite/467a91d62b5d66d82341 to your computer and use it in GitHub Desktop. 
    Getting a list of routes from an Express 4 application
  
        
  
    
      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 _ = require('lodash') | |
| var routes = _.map(_.filter(app._router.stack, function(item){ | |
| return (item.route != undefined) | |
| }), function(route){ | |
| return { | |
| path: route.route.path, | |
| method: Object.keys(route.route.methods)[0] | |
| } | |
| }) | |
| /** | |
| Sample output: | |
| { route : '/', method: 'get' }, | |
| { route : '/things', method: 'get' }, | |
| { route : '/things', method: 'post' }, | |
| { route : '/things/:id', method: 'get' }, | |
| ...etc | |
| **/ | |
| _.each(routes, function(route){ | |
| console.log(route.method.toUpperCase() + '\t' + route.path) | |
| }) | |
| /** | |
| Sample output: | |
| GET / | |
| GET /things | |
| POST /things | |
| GET /things/:id | |
| ...etc... | |
| **/ | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment