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);
}
});
}
});
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] }
}] */
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With