3

我有一个 Loopback 模型,我在该模型上使用 afterRemote 钩子将请求记录到敬业度。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();
    });
};
4

1 回答 1

3

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

或者

res.on('finish') 事件

于 2015-03-13T09:03:11.650 回答