Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to view all routes in a Hapi server

We are working on a node.js Hapi server that pulls in a list of routes from a MongoDB database and sets up said routes for servicing. With this, there is the potential for the server to fail because of duplicate route entries in the database.

I've tried to look, but have failed to find a way to check for duplicate routes in Hapi.

Is it possible to get a list of routes a Hapi server is currently serving?

Is there an error check I can make that is prettier than a standard try/catch block when attempting to build the routes coming from the MongoDB?

The code that sets up the routes is below; please see my comments in the code for where I need to handle the error.

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);
            }
        });
    }

});
like image 850
J2N Avatar asked Oct 07 '14 19:10

J2N


1 Answers

Maybe server.table() would help you? It returns a copy of the routing table. An example from the docs page:

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] }
    }] */
like image 112
Rodrigo Medeiros Avatar answered Sep 21 '22 13:09

Rodrigo Medeiros