2

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.

1 Answer 1

2

Readable streams don't on their own support transforms.

You'll want to create a stream.Transform and define its _transform(), then chain it between req and res with another .pipe():

var stream = require('stream');

var server = http.createServer(function(req, res) {
  // ...

  var uppercase = new stream.Transform();
  uppercase._transform = function (data, enc, cb) {
    cb(null, data.toString().toUpperCase());  
  };

  req.pipe(uppercase).pipe(res);
})
Sign up to request clarification or add additional context in comments.

2 Comments

Ahh. That makes sense. 1 follow-up question: I've read that the streams module is unstable (I know it has a 2 stability index) and people suggest other libs in it's place. Is it generally good to trust that advice or is it more case-by-case and streams can be somewhat relied upon?
and is this generally true for all stream types in Node? are transforms separated to their own area of concern?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.