I'm working through some Learnyounode/nodeschool examples and am hoping to get a better understanding of stream transforms. I've created a simple http server in node using http.createServer(). My hope is to be able to understand streams well enough first without having to lean on helper libraries like through or through2. I'd love to it more 'natively-to-node' if at all possible and gain a better understanding of the streaming notion in node that way.
Given a POST request (which is guaranteed in this learning case), I'm wanting to transform the characters in the body of the request to uppercase characters using .toUpperCase(). I'm familiar enough with JS to find that part easy, but my confusion stems mainly from why the stream I am .pipe()-ing to the response isn't transformed.
I'm able to log() out the transformed characters, but for some reason I'm not seeing they don't get returned as actually-transformed characters. I know that .toUpperCase() doesn't affect the actual string itself, but I am returning that value, so it that doesn't seem to be where my confusion lies (but please help me clarify if I'm wrong about that). Thanks everyone!
Sample input on POST:
bob
Joe
larry
Expected output:
BOB
JOE
LARRY
Actual output:
bob
Joe
larry
My simple server:
var http = require('http')
var port = process.argv[2]
var server = http.createServer(function(req, res) {
if (req.method != 'POST') {
console.log('You have failed to comply with our stringent demands');
return false
}
req.on('data', function(chunk) {
return chunk.toString().toUpperCase()
}).pipe(res);
})
server.listen(port)
EDIT What I learned: Per the accepted answer below, not all streams in node support transforms on their own. When you need to perform a transform on a readableStream (or other similar type), create a stream.transform() and pipe that instead.