Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Build a Simple Dynamic Site with Node.js Handling Routes in Node.js Populating User Information

Brian Anstett
Brian Anstett
5,831 Points

Does Node convert buffers to strings "behind the scenes" when appending to var body?

In Node.js basics, we learned about buffers and how we need to convert them to strings. However, I noticed that this conversion seems to be happening automatically when we append "data" to "body." Does Node convert buffers to string behind the scenes making the toString() method unnecessary?

//Read the data
        response.on('data', function (chunk) {
            console.log(chunk);
            body += chunk;
            console.log(body);

            profileEmitter.emit("data", chunk);
        });
<Buffer 2e 63 6f 6d 2f 62 61 64 67 65 73 5f 63 73 73 5f 66 6c 65 78 62 6f 78 5f 6c 61 79 6f 75 74 5f 73 74 61 67 65 33 2e 70 6e 67 22 2c 22 65 61 72 6e 65 64 5f ...>
undefined{"name":"Brian Anstett","profile_name":"briananstett", etc...
nico dev
nico dev
20,364 Points

Wow, great question you brought up right there. Haven't noticed that.

While I can't give you what you're probably looking for, the answer from someone who knows a lot about this and has experience, I still can somehow confirm what you suspect, having made many different trials, like this:

response.on('data', function (chunk) {
            console.log(typeof chunk);
            console.log(typeof body);
            body += chunk;
            console.log('After each chunk:');
            console.log(typeof body);
            profileEmitter.emit("data", chunk);
        });

// Outputs:
// object --> So this was the first chunk before being sent to body, right?
// undefined --> body before receiving the first chunk, yes?
// After each chunk:
// string --> Here's body after the first (buffer(?)) chunk...
// object --> Well, just chunk before the next one...
// string --> body before the next one, and etc...
// After each chunk:

// etc...

// After each chunk:
// string
// Output ended there.

I thought it could be related to the var body having been initialized as a string, but I tried declaring it inside the 'data' block (and without initializing it) and it's still the same. The code above shows the output. Takes the buffer and magically becomes into a string. can't find any explanation of this in the docs, either.

Of course, if I assign body as a string, it will appear as a string in the first of the outputs above (tried that, too), but the rest will be the same.

I am also a little surprised about the fact that the (buffer) object is assigned to the string variable just like that, no conflict of type. A little lost here. Hope someone will answer you here. I am eager to understand it better.