Skip to content

Instantly share code, notes, and snippets.

@thlorenz
Created August 15, 2013 12:57
Show Gist options
  • Save thlorenz/6240625 to your computer and use it in GitHub Desktop.
Save thlorenz/6240625 to your computer and use it in GitHub Desktop.
Using hooker to inject logs about events and/or http server responses
// npm install hooker
var hooker = require('hooker');
function hookEmitter () {
var EventEmitter = require('events').EventEmitter;
// to only hook emit: hooker.hook(EventEmitter.prototype, 'emit', function () {
hooker.hook(EventEmitter.prototype, function () {
console.log('args', arguments);
});
var emitter = new EventEmitter();
emitter.emit('data', 'hello');
}
// hookEmitter();
function hookHttp () {
var http = require('http');
hooker.hook(http.ServerResponse.prototype, function () {
console.log('args', arguments);
});
var server = http.createServer(function (req, res) {
var body = 'hello world';
res.writeHead(200, { 'Content-Length': body.length, 'Content-Type': 'text/plain' });
res.end(body);
});
server.listen(3000);
console.log('now: curl localhost:3000');
}
hookHttp();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment