Created
May 12, 2016 03:32
-
-
Save jonathanwork/2812c0530945a6ee0054efac45a26629 to your computer and use it in GitHub Desktop.
proxy-stream.js
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 port = 8080, | |
fs = require("fs"), | |
http = require("http"), | |
server = http.createServer() | |
function forEachLine(chunks, callback) { | |
var buffer = chunks.join("") | |
buffer.substr(0, buffer.lastIndexOf("\n")).split("\n").forEach(callback) | |
return buffer.substr(buffer.lastIndexOf("\n") + 1).split("\n") | |
} | |
server.on("request", function(request, response) { | |
var chunks = [], | |
output = fs.createWriteStream("./output") | |
request.on("data", function(chunk) { | |
chunks = forEachLine(chunks.concat(chunk), function(line) { | |
output.write(parseInt(line, 10) * 2) | |
output.write("\n") | |
}) | |
}) | |
request.on("end", function() { | |
response.writeHead(200, { "Content-Type": "plain/text" }) | |
response.end("OK\n") | |
output.end() | |
server.close() | |
}) | |
}) | |
server.listen(port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment