Skip to content

Instantly share code, notes, and snippets.

@NathanLBCooper
Last active March 15, 2019 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NathanLBCooper/1ab7b762cf16bb301a9fac79472c4ec4 to your computer and use it in GitHub Desktop.
Save NathanLBCooper/1ab7b762cf16bb301a9fac79472c4ec4 to your computer and use it in GitHub Desktop.
Crude express proxy that spies on traffic
var express = require("express");
var httpProxy = require("http-proxy");
var stream = require("stream");
var api = "http://localhost:4201";
var apiProxy = httpProxy.createProxyServer({
target: api
});
var app = express();
/**
* Make body readable
*/
app.use(function (req, res, next) {
if (req.method != "POST" && req.method != "PUT" && req.method != "PATCH") {
next();
return;
}
var buffer = '';
req.on('data', function (data) {
buffer += data;
});
req.on('end', function () {
req.body = buffer;
var bufferStream = new stream.PassThrough();
bufferStream.end(new Buffer(buffer));
req.bodyStream = bufferStream;
next();
});
});
/**
* Log
*/
app.use(function (req, res, next) {
// todo, some kind of decision on whether to log. todo Whether that is here, or via checking some kind of header
console.log(req.originalUrl);
if (req.body != null) {
console.log(req.body);
}
next();
});
/**
* Proxy request (given we're already reading the body)
*/
app.all("/*", function (req, res) {
apiProxy.web(req, res, { buffer: req.bodyStream });
});
app.listen(4202, function () {
console.log("Snooping Proxy listening on port 4202!");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment