12

私たちは、MongoDB データベースからルートのリストを取得し、サービスのために上記のルートを設定する node.js Hapi サーバーに取り組んでいます。これにより、データベース内のルート エントリが重複するために、サーバーが失敗する可能性があります。

調べてみましたが、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);
            }
        });
    }

});
4

4 に答える 4

22

多分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] }
    }] */
于 2014-10-08T16:04:53.587 に答える
6

私はHapi v17.6.0を使用しています:

server.table().forEach((route) => console.log(`${route.method}\t${route.path}`));
于 2018-09-26T14:23:01.900 に答える
2

私は 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);
 });
于 2016-10-18T19:59:12.430 に答える