HTTP Sniffer – listening to the HTTP conversation
The events emitted by the HTTPServer object can be used for additional purposes beyond the immediate task of delivering a web application. The following code demonstrates a useful module that listens to all the HTTP Server events. It could be a useful debugging tool, which also demonstrates how HTTP server objects operate.
Node.js's HTTP Server object is an EventEmitter
and the HTTP Sniffer simply listens to every server event, printing out information pertinent to each event.
What we're about to do is:
- Create a module,Â
httpsniffer
, that prints information about HTTP requests. - Add that module to theÂ
server.js
 script we just created. - Rerun that server to view a trace of HTTP activity.
Create a file namedhttpsniffer.js
containing the following code:
const util = require('util'); const url = require('url'); const timestamp = () => { return new Date().toISOString(); } exports.sniffOn = function(server) { server.on('request', (req, res) =>...