有没有办法查看Hapi服务器中的所有路由

我们正在研究一个node.js Hapi服务器,该服务器从MongoDB数据库中提取路由列表,并设置所需的服务路由。 因此,由于数据库中存在重复的路由条目,服务器可能会失败。

我试过看,但没有find一种方法来检查哈皮重复路线。

是否有可能获得Hapi服务器当前正在服务的路由列表?

在尝试构build来自MongoDB的路由时,是否有错误检查可以比标准的try / catch块更漂亮?

下面是设置路由的代码; 请在代码中查看我的意见,了解我需要处理错误的位置。

MySchema.find({}, function (err, stubs) { if (err) { console.log('error while loading'); return; } for (var i = 0; i < stubs.length; i++) { var bodyMessage = stubs[i].body; // This is where we can fail, if only I could make a // check for the route here server.route({ method: stubs[i].method, path: stubs[i].path, handler: function (request, reply) { reply(bodyMessage); } }); } }); 

也许server.table()会帮助你? 它返回一个路由表的副本。 来自文档页面的示例:

 var table = server.table() console.log(table); /* Output: [{ method: 'get', path: '/test/{p}/end', settings: { handler: [Function], method: 'get', plugins: {}, app: {}, validate: {}, payload: { output: 'stream' }, auth: undefined, cache: [Object] } }] */ 

我正在使用Hapi 15.1.1版本,这对我很有用:

  // server.select if you have more than one connection const table = server.select('api').table(); let routes = []; table[0].table.forEach((route) => { // you can push here the route to routes array routes.push(route); }); 

要修改一个特定的路由,你可以通过连接对象内的.match()方法来查找它们。

 var routeObj = server.connections[0].match('get', '/example', '<optional host>') routeObj.settings.handler = function(req, reply){ reply({"statusCode":404,"error":"Not Found"}) } 

如果你有多个连接循环通过他们每个改变。 上面的路由处理程序更改为404,因为你不应该删除路由。

路由存储在hapi / node_modules / callpath索引的对象中