如何获取我在restify服务器中使用的所有路由的列表

时间:2021-10-05 17:03:02

I have a app designed as follows;

我有一个如下设计的应用程序;

//server.js =====================================================
var restify = require('restify'),
    route1 = require('./routes/route1),
    route2 = require('./routes/route2),
    ....
    ....
    ....

var server = restify.createServer({
    name: 'xyz_server'
  });

route1(server);
route2(server);

Now each route file looks like belwo

现在每个路径文件看起来像belwo

   //route1.js =====================================================

   module.exports = function(server) {
      server.get('/someRoute',function(req,res,next){
                //.. do something
        });
      server.get('/anotherRoute',function(req,res,next){
                 //..something else
       });

   }; 

Now the issue is tht we have dozen's of route files and hundreds of routes in total. There are multiple developers working on this project and several routes are being added daily.

现在问题是我们有十几个路由文件和总共数百条路由。有多个开发人员正在处理这个项目,每天都会添加几个路由。

Is there a function in restify gives me a list of all routes in the system ?

在restify中有一个函数给我一个系统中所有路由的列表吗?

What i am looking for is something like:

我正在寻找的是:

server.listAllRoutes();

Is anyone aware of this ?

有人知道吗?

1 个解决方案

#1


3  

Try something like this

尝试这样的事情

function listAllRoutes(server){
  console.log('GET paths:');
  server.router.routes.GET.forEach(
    function(value){console.log(value.spec.path);}
    );
  console.log('PUT paths:');
  server.router.routes.PUT.forEach(
    function(value){console.log(value.spec.path);}
    );
}

listAllRoutes(server);

This should list all GET and PUT paths, adding POST and DELETE should be easy :)

这应列出所有GET和PUT路径,添加POST和DELETE应该很容易:)

#1


3  

Try something like this

尝试这样的事情

function listAllRoutes(server){
  console.log('GET paths:');
  server.router.routes.GET.forEach(
    function(value){console.log(value.spec.path);}
    );
  console.log('PUT paths:');
  server.router.routes.PUT.forEach(
    function(value){console.log(value.spec.path);}
    );
}

listAllRoutes(server);

This should list all GET and PUT paths, adding POST and DELETE should be easy :)

这应列出所有GET和PUT路径,添加POST和DELETE应该很容易:)