Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.