1

I just started watching some node tutorials and I wanted help understanding the response and request streams that I get from http.createServer(). Response & Request are streams, so does that mean than Node.js sends and recieves data in chunks?

For example, if I called

res.write("test1");

res.write("test2");

res.end();

would it only write both those things when I call end() or would it flush to the stream and send to the client making the request as and when I call write()?

Another example to elaborate on my question is if I had a txt file with a lot of plaintext data, then I setup a read stream that pipes data from that file to the res object would it pipe that data in chunks or do it once everything is in the buffer.

I guess my question also applies to the request object. For instance, is the body of the request built up packet by packet and streamed to the server or is it all sent at once, and node just chooses to make us use a stream to access it.

Thanks alot!

  • It depends. ... https://stackoverflow.com/questions/18857693/does-express-js-support-sending-unbuffered-progressively-flushed-responses – Jonas Wilms Jul 30 '17 at 09:41

1 Answers1

1

The first time response.write() is called, it will send the buffered header information and the first chunk of the body to the client. The second time response.write() is called, Node.js assumes data will be streamed, and sends the new data separately. That is, the response is buffered up to the first chunk of the body.

full foc

So basically, if you .write() a small piece of data, it may be buffered until theres a complete chunk or .end() is called. If .write() already has the size of a chunk, it will be transmitted immeadiately.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • I see, so lemme clarify, if I response.write() twice, then node will send the second chunk of data separately. Is that why we have res.end() so it knows when I actually am not going to send anymore chunks, and can close the connection. I guess my only other question other than that clarification is also are requests buffered and streamed by default also? I assume this is the case and i just have to listen with on('data'). – user2814533 Jul 30 '17 at 09:50
  • @user2814533 yes – Jonas Wilms Jul 30 '17 at 09:52
  • umm can i ask one more thing then. when i don't include res.end() and i visit localhost:3000 i don't get anything, but when i put .end() it shows me the data I wrote; however, based on what you said "The first time response.write() is called, it will send the buffered header information and the first chunk of the body to the client." shouldn't I get the data of the first res.write() regardless of whether res.end() is included? – user2814533 Jul 30 '17 at 10:16
  • @user2814533 that may also depend on your browser, if it shows the content already while the page is still loading. – Jonas Wilms Jul 30 '17 at 10:22
  • i see so the connection isn't stopped until end is called, so the browser may just be saying i won't load till i have everything. did i understand you right? if so, i think i've had all my questions answered. – user2814533 Jul 30 '17 at 10:22
  • @user2814533 yes that may be the issue. However im not shure about that, and its difficult to answer. – Jonas Wilms Jul 30 '17 at 10:24