我们正在开发一个 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);
}
});
}
});