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!

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 yes
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 that may also depend on your browser, if it shows the content already while the page is still loading.
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.
|

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.