Get all route in Express

    0

    0

    The code prints the routes and the handles of the routes. The code also prints the methods of the routes.

    run at server.js
    
    function print (path, layer) {
      if (layer.route) {
        layer.route.stack.forEach(print.bind(null, path.concat(split(layer.route.path))))
      } else if (layer.name === 'router' && layer.handle.stack) {
        layer.handle.stack.forEach(print.bind(null, path.concat(split(layer.regexp))))
      } else if (layer.method) {
        console.log('%s /%s',
          layer.method.toUpperCase(),
          path.concat(split(layer.regexp)).filter(Boolean).join('/'))
      }
    }
    
    function split (thing) {
      if (typeof thing === 'string') {
        return thing.split('/')
      } else if (thing.fast_slash) {
        return ''
      } else {
        var match = thing.toString()
          .replace('\\/?', '')
          .replace('(?=\\/|$)', '$')
          .match(/^\/\^((?:\\[.*+?^${}()|[\]\\\/]|[^.*+?^${}()|[\]\\\/])*)\$\//)
        return match
          ? match[1].replace(/\\(.)/g, '$1').split('/')
          : '<complex:' + thing.toString() + '>'
      }
    }
    
    app._router.stack.forEach(print.bind(null, []))
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.