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