I am using a module called request
(https://www.npmjs.com/package/request) to perform series of http requests on my application. Now I am trying to add a logging functionality to it but cannot seem to figure out how to capture the events from the request module. I can do the event capturing like so:
var request = require('request');
request('http://www.stackoverflow.com').on('request', function (data) {
console.log(data.method + ":" + data._headers.host);
}).on('response', function (data) {
console.log("Status: " + data.statusCode);
});
This will produce output of:
$ node test.js
GET:www.stackoverflow.com
GET:stackoverflow.com
Status: 200
How can I capture the events without specifying it after the request()
call? Ideally I would like to only have to require request
module and some other file which would contain all the listeners for request events and start using request
without worrying about the event handling. So the end result would look in the code files something like:
var request = require('request');
var requestLogger = require('reqLogger')(request);
According to the request
readme, there are at least a few different ways to get debug information from request
. The third solution in particular (using request-debug
) seems to get you exactly what you're currently trying to do.