Created
October 8, 2015 14:02
-
-
Save aitormagan/7e4c5cbfa4765b84d965 to your computer and use it in GitHub Desktop.
Node Proxy Transparent to Log external (iOS, Android) HTTP requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Logging the status code will be interesting ¬¬