我可以在Loopback afterRemote挂钩中找到响应标头吗?

时间:2015-03-07 16:38:14

标签: express loopbackjs strongloop keen-io

我有一个Loopback模型,我使用afterRemote钩子将请求记录到keen.io。 http://docs.strongloop.com/display/public/LB/Remote+hooks#Remotehooks-ctx.result

我还使用响应时间包将响应时间标头添加到响应中。 https://github.com/expressjs/response-time

这工作正常,我希望我无法弄清楚如何在响应中找到X-Response-Time标头,以便将其记录到keen.io。

我可以通过以下任何方式访问响应标头吗?

module.exports = function(Studio) {
    var isStatic = true;
    var isNotStatic = false;
    Studio.disableRemoteMethod('deleteById', isStatic); // DELETE /Studios/{id}
    Studio.disableRemoteMethod('create', isStatic); // POST /Studios
    Studio.disableRemoteMethod('upsert', isStatic); // PUT /Studios
    Studio.disableRemoteMethod('updateAll', isStatic); // POST /Studios/update
    Studio.disableRemoteMethod('updateAttributes', isNotStatic); // PUT /Studios/{id}
    Studio.disableRemoteMethod('__create__ListenNps', isNotStatic);
    Studio.disableRemoteMethod('__delete__ListenNps', isNotStatic);
    Studio.disableRemoteMethod('__destroyById__ListenNps', isNotStatic);
    Studio.disableRemoteMethod('__updateById__ListenNps', isNotStatic);

    Studio.afterRemote('*', function(ctx, affectedModelInstance, next) {
        var Keen = require('keen-js');

        var client = new Keen({
            projectId: "myid",
            writeKey: "mykey"
        });

        var queryEvent = {
            ip: ctx.req.ip,
            baseUrl: ctx.req.baseUrl,
            url: ctx.req.url,
            route: ctx.req.route,
            query: ctx.req.query,
            method: ctx.methodString,
            // response: ctx.result.???, What can I do here to get to the response headers? Specifically X-Response-Time
            keen: {
                timestamp: new Date().toISOString()
            }
        };

        client.addEvent("queries", queryEvent, function(err, res) {
            if (err) {
                console.log(err)
            } else {
                console.log(res)
            }
        });
        next();
    });
};

1 个答案:

答案 0 :(得分:3)

尝试使用ctx.res.getHeader('X-Response-Time') method

聆听res.on('finish') event