express get all endpoints

Solutions on MaxInterview for express get all endpoints by the best coders in the world

showing results for - "express get all endpoints"
Dimitri
11 Jan 2017
1const routes = {}
2function routerRecursion(middleware, pointer, currentName) {
3  if (middleware.route) { // routes registered directly on the app
4    if (!Array.isArray(pointer['routes'])) {
5      pointer['routes'] = []
6    }
7    const routeObj = {
8      path: middleware.route.path,
9      method: middleware.route.stack[0].method
10    }
11    pointer['routes'].push(routeObj)
12  } else if (middleware.name === 'router') { // inside router
13    const current = middleware.regexp.toString().replace(/\/\^\\\//, '').replace(/\\\/\?\(\?\=\\\/\|\$\)\/\i/, '')
14    pointer[current] = {}
15    middleware.handle.stack.forEach(function (handler) {
16      routerRecursion(handler, pointer[current], current)
17    });
18  }
19}
20app._router.stack.forEach(function (middleware) {
21  routerRecursion(middleware, routes, 'main')
22});
23console.log(routes);
similar questions
queries leading to this page
express get all endpoints