JLaw JLaw - 1 year ago 52
Node.js Question

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

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
method: stubs[i].method,
path: stubs[i].path,

handler: function (request, reply) {


Answer Source

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

var table = server.table()

/*  Output:

      method: 'get',
      path: '/test/{p}/end',
      settings: {
        handler: [Function],
        method: 'get',
        plugins: {},
        app: {},
        validate: {},
        payload: { output: 'stream' },
        auth: undefined,
        cache: [Object] }
    }] */