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 Simple Server in Node.js HTTP Server Review

Theo Bouwman
Theo Bouwman
2,812 Points

The answer is "Goodbye World"? And why isn't it?

I thought when response.end(); is writed, the whole program will end and stop making response.

1 Answer

Nathan Brenner
Nathan Brenner
35,844 Points

Basically response.end() doesn't get called until after the argument set by the SetTimeout function.

It's kinda like there are two cages on a table. The cage that has the setTimeout function has a sheet covering it up so that the javaScript interpreter can't see it...until 1000 milliseconds of time have passed since the http.createServer method has started. Once the http.createServer has been initiated, response.write("Hello World/n") is visible to the javaScript interpreter instantly. A second later, the sheet covering the response.end("Goodbye World\n") is removed and becomes visible to the javaScript interpreter. Response.end() works the same way as response.write() if there is any data that is set as an parameter. After the response outputs the data, the .end method tells the server the request has been completed.