我想为每个请求向请求对象添加数据。因此我订阅了onRequest
事件并且正在做:
server.ext('onRequest', (request, reply) => {
console.log('I AM CALLED');
const marketCode : Market = request.params["market-code"];
const operatorCode : Operator = request.params["operator-code"];
request.operatorContext = new OperatorContext(
operatorEndpointEnv,
operatorCode,
marketCode
);
console.log(request.operatorContext); // will be defined
return reply.continue();
});
然而,当我的控制器:
function apiStatus(req, reply) {
console.log(req.operatorContext); // will be undefined
reply.status(204).end();
}
收到调用,请求只是普通request.operatorContext
未定义,但它在添加定义的扩展的上下文中设置,因为我可以看到它已记录:
I AM CALLED
OperatorContext { environment: 'test', operator: undefined, market: undefined }
undefined
如何将自定义数据加载到请求对象?