Last active
December 3, 2019 22:01
-
-
Save Masquerade-Circus/7ed1d54ecd85ebd7f1ad1de7c3aea4a5 to your computer and use it in GitHub Desktop.
Log all express routes including those on sub routers.
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
import express from 'express'; | |
let App = express(), | |
app_port = process.env.app_port || 11337, | |
app_host = process.env.app_host || '127.0.0.1'; | |
App.listen(app_port, () => { | |
console.log(`Server app running on http://${app_host}:${app_port}`); | |
}); | |
var getRoutes = function (stack, prefix = '', routesArray = []) { | |
for (let i in stack) { | |
let obj = {}; | |
if (stack[i].route && stack[i].route.path) { | |
obj.path = prefix + stack[i].route.path; | |
obj.method = Object.keys(stack[i].route.methods).shift(); | |
routesArray.push(obj); | |
} else if (stack[i].name === 'router') { | |
let path = stack[i].path || ''; | |
getRoutes(stack[i].handle.stack, prefix + path, routesArray); | |
} | |
} | |
return routesArray; | |
}; | |
console.log(getRoutes(App._router.stack)); |
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
// For express >= 4.16.x | |
import express from 'express'; | |
let App = express(), | |
app_port = process.env.app_port || 11337, | |
app_host = process.env.app_host || '127.0.0.1'; | |
App.listen(app_port, () => { | |
console.log(`Server app running on http://${app_host}:${app_port}`); | |
}); | |
const getRoutes = function (stack, prefix = '', routesArray = [], routesObj = {}) { | |
for (let i in stack) { | |
let obj = {}; | |
if (stack[i].route && stack[i].route.path) { | |
obj.path = (prefix + stack[i].route.path) | |
.replace(/\/\//g, '/') | |
.replace(/\\\//g, '/') | |
.replace(/\/$/, ''); | |
obj.method = Object.keys(stack[i].route.methods).shift(); | |
if (routesObj[obj.path] === undefined) { | |
routesObj[obj.path] = {}; | |
} | |
if (routesObj[obj.path][obj.method] === undefined) { | |
routesObj[obj.path][obj.method] = 0; | |
} | |
routesObj[obj.path][obj.method] += 1; | |
routesArray.push(obj); | |
} else if (stack[i].name === 'router') { | |
let path = stack[i] | |
.regexp | |
.toString() | |
.replace(/^\/\^\\/, '') | |
.replace(/(\?\(\?=\\\/\|\$\)\/i)/, '') | |
.replace(/\?\$\/i$/, '') | |
.replace(/\\\/$/, '') | |
.replace(/\\\./g, '.'); | |
let pref = (prefix + path) | |
.replace(/\/\//g, '/') | |
.replace(/\\\//g, '/') | |
.replace(/\/$/, ''); | |
getRoutes(stack[i].handle.stack, pref, routesArray, routesObj); | |
} | |
} | |
return { array: routesArray, obj: routesObj }; | |
}; | |
console.log(getRoutes(App._router.stack)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment