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 Reading from Files

Why does code not work without "response.end('some string')?"

I wrote the code exactly as it appeared in this exercise yet on every test I do, the browser hangs for about 30 seconds and eventually just times out. I checked the network tab and it says the request failed.

To get my code to work, I have to add 'response.end("something");' to all of my callback functions.

Why is it that Chalkers' code loads nearly immediately with no hang, and mine does not?

Here is a snapshot of my workspace https://w.trhou.se/zfgob542bv

2 Answers

Seth Kroger
Seth Kroger
56,406 Points

response.end() is always needed to let the server know we're done building the response send, anything left and close browser connection. It doesn't need an argument but is it needed to wrap thing up. You'll see that Andrew goes back near the end of the video and adds response.end()'s after the view calls.

Nikhil Khandelwal
Nikhil Khandelwal
9,371 Points

The explanation on nodejs.org is : response.end([data][, encoding][, callback]) "If data is specified, it is equivalent to calling response.write(data, encoding) followed by response.end(callback)". All the argument in the end function are optional.

As you have written response.end("footer \n"); response.end("end")//delete if necessary ----- " ; " missing at end of function response.end("end!"); //delete if needed

Hope this help