O'Reilly logo

Node Web Development by David Herron

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

HTTP Sniffer—listening to the HTTP conversation

Now let's start working with the HTTP objects by creating a useful class which listens to all events emitted by an HTTP Server object. It could be a useful debugging tool which also demonstrates how HTTP server objects operate.

Node's HTTP Server object is an EventEmitter and the HTTP Sniffer simply listens to every server event, printing out information pertinent to each event.

Create a file named httpsniffer.js containing the following:

var util = require('util'); var url = require('url'); exports.sniffOn = function(server) { server.on('request', function(req, res) { util.log('e_request'); util.log(reqToString(req)); }); server.on('close', function(errno) { util.log('e_close errno='+ errno); }); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required