Logging LoopBack API requests to Keen.io

duffn
1 min readMar 14, 2015

I’ve been experimenting with LoopBack recently, using it to prototype an API. It really is quite amazing how quickly you can get a full-fledged API up and running with LoopBack. Just follow the getting started guide and you’ll see what I mean.

After getting a pretty decent API up and running, I began testing a few different ways of logging API requests. I still haven’t quite settled on the way that I want to do this yet, but one issue that I did encounter was trying to get the response time of the call from expressjs/response-time.

Response-time is a great, little Node.js module that automatically adds an X-Response-Time header to your responses to indicate the time in milliseconds that the request took.

The issue that I was running into was that I could not figure out how to properly get to that header in the LoopBack afterRemote method that I was using to log requests. Thanks to an SO user, however, I was able to eventually get to what I wanted. The key was that the response wasn’t available when I was trying to log the query — I needed to use the res.on(‘finish’) event to get the response headers.

--

--

duffn

I write about Python, Go, Node.js, cloud infrastructure, and data. https://duffn.github.io