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 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,412 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