简体   繁体   English

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

[英]How to get list of all routes I am using in restify server

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 ? 有人知道吗?

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应该很容易:)

There is a router.getRoutes() method, but it returns an object which is not the best to work with for listing things. 有一个router.getRoutes()方法,但是它返回的对象不是最好用于列出事物的对象。 You could fiddle around with that to turn it into an array with the shape that you like. 您可以摆弄它,将其变成具有所需形状的数组。

Alternatively, you can access all the routes as an array and then map them, even better if you use a lib like better-console to give you console.table in node. 另外,您可以将所有路由作为数组访问,然后将它们映射,如果您使用诸如better-console类的lib在node中提供console.table ,则better-console The following is working nicely for me in restify@8.3.0 : 以下代码在restify@8.3.0对我来说很restify@8.3.0

import console from 'better-console';

function listRoutes(server) {
  const { routes } = server.router._registry._findMyWay; // beware these are probably meant to be private APIs, they could be subject to change
  const mapped = routes.map(({ method, path }) => ({ method, path }));

  console.table(mapped.sort((a, b) => a.method > b.method));
}

2019 update: server.router.routes is no longer available instead we have server.router.getRoutes() which returns a Map. 2019更新: server.router.routes不再可用,而是我们有server.router.getRoutes()返回地图。 So we can log all the routes using: 因此,我们可以使用以下命令记录所有路线:

function listAllRoutes(server) {
  Object.values(server.router.getRoutes()).forEach(value =>
    console.log(
      `ENDPOINT REGISTERED :: ${value.method} :: ${server.url}${value.path}`
    )
  );
}

http://restify.com/docs/server-api/#server http://restify.com/docs/server-api/#server

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM