I am getting acquainted with streams in nodejs and I have a question:
I have what I think is the simplest possible nodejs "echo server" ie a server that simple pipes back to the response stream whatever it receives via the request stream. It works but with a caveat. The client only receives the data back after it closes the submitting stream. Here is the server code:
var http = require('http')
var server = http.createServer(function (req, res) {
req.pipe(res);
});
server.listen(8000);
And here is how I test it:
Doing the following works just fine
term1> node server.js&
term2> echo 'hello world!'|curl --no-buffer --data-binary @- 'http://localhost:8000'
hello world!
However, it only works because echo closes its output file descriptor after being done, ie., the server will not write anything until after its client is done sending stuff:
term2>
term2> yes|curl --no-buffer --data-binary @- 'http://localhost:8000'
(here this line gets stuck for ever)
I would expect that yes will fill stream buffers pretty fast so I would start seeing y's coming back pretty fast. Unfortunately they never do.
Is this expected? How should I use the streams/pipes so as they have the desired effect? By the way I don't care whether the output would come back in chunks... I understand that that would be results of streams (or the underlying file i/o) doing their buffering magic.
Thank you for your help
Ok,
I think I figured out what I was doing wrong.
The server.js was working fine... it was curl that was breaking the pipeline.
Apparently, the @- option treats stdin like a file ,ie reads its full content and then submits it. I couldn't find how to make curl pipe the contents as they are being read. I think its not feasible because curl wants to treat the contents of the file as a normal HTTP post form value..
Anyway, I modified the example above using a simple nodejs client that was doing what I wanted:
term2> cat client.js
var http = require('http');
var req = http.request('http://localhost:8000', function(res) {
res.pipe(process.stdout);
});
process.stdin.pipe(req);
term2> yes|node client.js
y
y
...
nodejs streams and pipes rock!!