This is a simple proxy server that sniffs traffic and writes to the console:
var http = require('http'), httpProxy = require('http-proxy'); // // Create a proxy server with custom application logic // var proxy = httpProxy.createProxyServer({}); // assign events proxy.on('proxyRes', function (proxyRes, req, res) { // collect response data var proxyResData=''; proxyRes.on('data', function (chunk) { proxyResData +=chunk; }); proxyRes.on('end',function () { var snifferData = { request:{ data:req.body, headers:req.headers, url:req.url, method:req.method}, response:{ data:proxyResData, headers:proxyRes.headers, statusCode:proxyRes.statusCode} }; console.log(snifferData); }); // console.log('RAW Response from the target', JSON.stringify(proxyRes.headers, true, 2)); }); proxy.on('proxyReq', function(proxyReq, req, res, options) { // collect request data req.body=''; req.on('data', function (chunk) { req.body +=chunk; }); req.on('end', function () { }); }); proxy.on('error', function(err) { console.error(err); }); // run the proxy server var server = http.createServer(function(req, res) { // every time a request comes proxy it: proxy.web(req, res, { target: 'http://localhost:4444' }); }); console.log("listening on port 5556") server.listen(5556);
source share