0

I'm unclear on what the .pipe() function does in node?

How could I use it to refactor any of the two functions below?

exports.collectData = function(req, callback) { var data = ""; req.on("data", function(chunk) { data += chunk; }) req.on("end", function() { callback(data); }) } http.createServer(function(req, res){ res.writeHead(200, {"Content-type": "text/plain"}); res.write("Howdy"); res.end(); }).listen(port); 

//new code from answer:

var fs = require("fs"); // Read File fs.createReadStream("input/people.json") // Write File .pipe(fs.createWriteStream("output/people.json")); 

1 Answer 1

5

.pipe connects the readable side of a stream with the writeable side of another stream:

readable.pipe(writable) 

I.e. it's a way to pass data from one stream to another. It's the Node equivalent to IUnix pipes:

foo | bar 

How could I use it to refactor any of the two functions below?

Since you don't seem to connect two streams there is no need to use .pipe.

Sign up to request clarification or add additional context in comments.

1 Comment

@devdropper87: Yes. I recommend to read github.com/substack/stream-handbook .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.