Skip to content

Instantly share code, notes, and snippets.

@aitormagan
Created October 8, 2015 14:02
Show Gist options
  • Save aitormagan/7e4c5cbfa4765b84d965 to your computer and use it in GitHub Desktop.
Save aitormagan/7e4c5cbfa4765b84d965 to your computer and use it in GitHub Desktop.
Node Proxy Transparent to Log external (iOS, Android) HTTP requests
var http = require("http");
var fs = require("fs");
var url = require("url")
http.createServer(function(clientRequest, clientResponse) {
var requestBody = "";
clientRequest.on("data", function(new_data) {
requestBody += new_data;
});
clientRequest.on("end", function(new_data) {
var parsedUrl = url.parse(clientRequest.url);
var requestOptions = {
host: parsedUrl["hostname"],
port: parsedUrl["port"],
path: parsedUrl["path"],
method: clientRequest.method,
headers: clientRequest.headers
}
var proxyRequest = http.request(requestOptions, function(proxyResponse) {
var responseBody = "";
proxyResponse.on("data", function(chunk) {
responseBody += chunk;
});
proxyResponse.on("end", function() {
clientResponse.writeHead(proxyResponse.statusCode, proxyResponse.headers);
clientResponse.write(responseBody);
clientResponse.end();
// Log request...
console.log("------------REQUEST RECEIVED------------")
console.log(" --> URL: " + clientRequest.url);
console.log(" --> METHOOD: " + clientRequest.method);
console.log(" --> HEADERS REQ: " + JSON.stringify(clientRequest.headers));
console.log(" --> BODY REQ: " + requestBody);
console.log(" --> HEADERS RES: " + JSON.stringify(proxyResponse.headers))
console.log(" --> RESPONSE: " + responseBody);
console.log("----------------------------------------\n\n");
})
});
proxyRequest.write(requestBody);
proxyRequest.end();
});
}).listen(parseInt(8000));
@aitormagan
Copy link
Author

Logging the status code will be interesting ¬¬

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment