nodeschool Streams adventure [HTTP SERVER] <p>I found a bit of intriguing behavior on an exercise about nodejs.</p> <p>The aim is to transform POST data of a request to uppercase and send back using stream.</p> <p>My problem is I'm not having the same behavior between these two pieces of code (the transform function just take the <code>buf</code> and <code>queue</code>it to uppercase) :</p> <pre><code>var server = http.createServer(function (req, res) { var tr = through(transform); if (req.method === 'POST') { req.pipe(tr).pipe(res); } }); </code></pre> <hr> <pre><code>var tr = through(transform); var server = http.createServer(function (req, res) { if (req.method === 'POST') { req.pipe(tr).pipe(res); } }); </code></pre> <p>The first one is correct and gives me :</p> <pre><code>ACTUAL EXPECTED ------ -------- "ON" "ON" "THEM" "THEM" "BUT," "BUT," ... </code></pre> <p>My version with the <code>tr</code>var outside :</p> <pre><code>ACTUAL EXPECTED ------ -------- "QUICK." "QUICK." "TARK'S" "TARK'S" "QUICK." !== "BIMBOOWOOD" "TARK'S" !== "BIMBOOWOOD" "BIMBOOWOOD" !== "SO" "BIMBOOWOOD" !== "SO" ... </code></pre> <p>For info the transform <code>function</code> :</p> <pre><code>function transform(data) { this.queue(data.toString().toUpperCase()); } </code></pre>