javascript 查看Express Routers(WIP)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 查看Express Routers(WIP)相关的知识,希望对你有一定的参考价值。

References:
- https://github.com/expressjs/express/issues/3308
- https://stackoverflow.com/questions/14934452/how-to-get-all-registered-routes-in-express/49152014
- https://thejackalofjavascript.com/list-all-rest-endpoints/
app._router.stack.forEach((rts) => {
  if (rts.route && rts.route.path){
    console.log(rts.route.path)
  }
})

app._router.stack.forEach(rts => {
  if (rts.handle.stack) {
    rts.handle.stack.forEach(rt => {
      console.log(' ', rt)
    });
  }
});

if (require.main === module) {
  // Listen for incoming connections
  app.listen(PORT, function () {
    console.info(`Server listening on ${this.address().port}`);
    /*
        var route, routes = [];
        app._router.stack.forEach(function (middleware) {
          if (middleware.route) {
            routes.push(middleware.route);
          } else if (middleware.name === 'router') {
            middleware.handle.stack.forEach(function (handler) {
              route = handler.route;
              route && routes.push(route);
            });
          }
        });
        console.log(route);
        console.log(routes);
     */
    app._router.stack.forEach(routes => {

      if (!routes.route && !routes.handle.stack) {
        console.log(routes.name)
      }

      if (routes.name === '<anonymous>') {
        console.log(routes.handle.toString())
      }

      if (routes.route) {
        console.log(routes.route.stack[0].method, routes.route.path)
      }

      if (routes.handle.stack) {
        console.log(routes.regexp.toString().replace('/^\\', '').replace('\\/?(?=\\/|$)/i', ''));
        routes.handle.stack.forEach(rts => {
          console.log('  ', rts.route.stack[0].method, rts.route.path)
        });
      }

    });


  }).on('error', err => {
    console.error(err);
  });
}

以上是关于javascript 查看Express Routers(WIP)的主要内容,如果未能解决你的问题,请参考以下文章

自十天学会NodeJs的实战运用,express框架的应用

如何在 express 项目中将 css 链接到 html?

javascript Express 1 - Découverted'express

使用 Express 时未加载 Javascript (Angular)

ubuntu 配置静态路由

javascript Express - 简单的路由演示