Last active
August 29, 2015 14:23
-
-
Save ajcrites/ff9248a8b4cdcafc108a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import {get, request} from "http"; | |
import {readFile, writeFile} from "fs"; | |
let host = "localhost"; | |
let port = 8917; | |
let body = ""; | |
get({host, port}, completeRequest(finalCb)); | |
function finalCb(err, result) { | |
if (err) { | |
console.error(err); | |
process.exit(1); | |
} | |
console.log(result); | |
} | |
function completeRequest(cb) { | |
return response => { | |
response.on("readable", () => { | |
body += response.read(); | |
}); | |
response.on("end", writeToFoo(body, cb)); | |
}; | |
} | |
function writeToFoo(body, cb) { | |
return () => { | |
writeFile(__dirname + "/foo", body, readFoo(cb)); | |
}; | |
} | |
function readFoo(cb) { | |
return err => { | |
if (err) { | |
return cb(err); | |
} | |
readFile(__dirname + "/foo", writeToBar(cb)); | |
}; | |
} | |
function writeToBar(cb) { | |
return (err, contents) => { | |
if (err) { | |
return cb(err); | |
} | |
writeFile(__dirname + "/bar", contents.toString().split("").reverse().join(""), postRequest(cb)); | |
}; | |
} | |
function postRequest(cb) { | |
return err => { | |
if (err) { | |
return cb(err); | |
} | |
request({host, port, method: "POST"}, completeRequest(cb)).end(); | |
}; | |
} | |
function completeRequest(cb) { | |
return response => { | |
cb(null, "operation complete"); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment