如何在hapi中向请求对象添加数据?

时间:2016-11-01 10:23:25

标签: node.js typescript request hapijs

我想为每个请求向请求对象添加数据。因此我订阅了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

如何将自定义数据加载到请求对象?

0 个答案:

没有答案