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 Creating a Simple Server

Help understanding the individual parts of createServer code so that I'm not only copying it.

More specifically these parts:

-What does writeHead mean, I'm assuming it's not a header.

-What is the second parameter for writeHead. ie -> {'Content-Type' : 'text/plain'} (an object right?) But what type of props/values does it takes?

-How do you discover the port number to use?

I really like this question and also like the response Andren provided.

The Mozilla document on HTTP headers and the header response documentation on Node.js are both great resources for figuring this out. Andrew did actually link them in the teachers notes on the video. To do more research to help fill in the gaps here are the links that may be useful:

MDN(Mozilla Developer Network) -- HTTP Headers

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers

Node.js API -- response.writeHead

https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

2 Answers

andren
andren
28,526 Points

What does writeHead mean, I'm assuming it's not a header.

Actually it is a header. It is a function used to write to the HTTP header that the server will respond with when a client connects to it.

What is the second parameter for writeHead. ie -> {'Content-Type' : 'text/plain'} (an object right?) But what type of props/values does it takes?

The key is an HTTP Header field and the value is whatever you want that field to be set to. You can find a list of the HTTP Header fields here.

How do you discover the port number to use?

The port number you specify decides which port on the server the HTTP server will listen to requests from. It can be pretty much any port you want, as long as you use the same port number to actually make requests to the server.

Most HTTP servers run on port 80 when used for production though, since that is considered the standard HTTP port. And is the port browsers will connect to by default when you browse to an HTTP address.