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

Quiz question. What is the output from the following code?

var http = require("http");

http.createServer(function(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'});

setTimeout(function(){ response.end('Goodbye World\n'); }, 1000);

response.write("Hello World\n"); }).listen(3000);

A. Goodbye World

B. Hello World Goodbye World

C. Hello World

D. Goodbye World Hello World

my choice was C. but, I received this message. Bummer! unfortunately, that answer is in correct. Why?. Hello World was the last thing printed, so doesn't a server always write out the last piece of code to the page? Which in this case would be Hello World.

1 Answer

Miguel Canas
seal-mask
.a{fill-rule:evenodd;}techdegree
Miguel Canas
Python Development Techdegree Student 11,470 Points

Hey Jason,

Checkout the third line of code...

var http = require("http");

http.createServer(function(request, response) { response.writeHead(200, {'Content-Type': 'text/plain'});

setTimeout(function(){ response.end('Goodbye World\n'); }, 1000);

response.write("Hello World\n"); }).listen(3000);

... The setTimeout([function], [milliseconds]) function will execute the given function after the given number of milliseconds. In this case, the response.write call for "Goodbye World" will delay execution for 1000 milliseconds, and the "Hello World" message will execute immediately.

Hence the correct answer would be B.

Make sense?

~ Miguel

Yes, I understand that now. Thanks.