Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Build a Simple Dynamic Site with Node.js Creating a Basic Template Engine in Node.js Binding Values

Why did the buffer work before trying to merge content with values?

I understood the part when Andrew said we needed to convert the buffer to a string.

I'm only wondering, why this didn't affect the website before? It was rendering in a way we could see the content with the placeholders "{{}}". I mean, we were already using the 'renderer.view()', and without the encoding. Maybe I'm missing something in the process. Thank you guys for any clarification!

1 Answer

Courtney Wilson
Courtney Wilson
8,031 Points

Just wanted to answer for anyone else who might be wondering the same thing. The response.write() method accepts a buffer as an argument. Buffer has a toString() method and the response.write() method uses this in its underlying code to write the buffer to the page as a string. In the mergeValues() function, we were trying to use fileContents as a string object by using String's replace() method. It had not been established as a string, only as a buffer and it was never converted until we added the encoding to the readFileSync() method. I'm not sure if it's bad practice or not but for testing purposes at least, you can just as easily use the toString() method on fileContents as an argument in mergeValues() and it would work.