我有多个以/networks/{networkId}/*
开头的端点。我不想在每个处理程序中寻找网络并对其执行一些额外的验证。有没有什么办法可以在更高层次上解决这个问题?防爆。插件/服务器方法等?HapiJS在处理程序中声明请求
在每一个处理程序,我有以下样板代码:
import networkRepo from 'common/repositories/network';
// handler.js
export default (req, reply) => {
return networkRepo.findById(req.params.networkId).then(network => {
// Logic to validate whether the logged user belongs to the network
// Logic where I need the network instance
});
}
最好的情况是:
// handler.js
export default (req, reply) => {
console.log(req.network); // This should be the network instance
}
你为什么打电话给reply.continue()?此外,您的测试看起来不正确 –
我修改了问题 – guidsen
我已经写了一个答案 –