Created
August 15, 2013 12:57
-
-
Save thlorenz/6240625 to your computer and use it in GitHub Desktop.
Using hooker to inject logs about events and/or http server responses
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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