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

J2N*_*J2N 11 mongodb node.js hapijs

我们正在开发一个node.js Hapi服务器,该服务器从MongoDB数据库中提取路由列表并设置所述路由以进行服务.这样,由于数据库中的路由条目重复,服务器可能会失败.

我试过看,但未能找到一种方法来检查Hapi中的重复路线.

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

在尝试构建来自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);
            }
        });
    }

});
Run Code Online (Sandbox Code Playgroud)

Rod*_*ros 18

也许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] }
    }] */
Run Code Online (Sandbox Code Playgroud)


J.C*_*.C. 6

我正在使用 Hapi v17.6.0:

server.table().forEach((route) => console.log(`${route.method}\t${route.path}`));
Run Code Online (Sandbox Code Playgroud)