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 HTTP Methods and Headers Sending Content Type Headers in Node.js

3 Answers

Collin Halliday
seal-mask
.a{fill-rule:evenodd;}techdegree
Collin Halliday
Full Stack JavaScript Techdegree Student 17,491 Points

Hey, Przemyslaw.

You are correct. The setHeader() method will not accept an object as an argument. It accepts a string representing the header's name as its first argument, and a string or array representing the header's value(s) as its second argument. The writeHead() method is the one that accepts an object as an optional argument. See https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_response_setheader_name_value See also, https://nodejs.org/dist/latest-v8.x/docs/api/http.html#http_response_writehead_statuscode_statusmessage_headers

I believe Chalkers uses the writeHead() method in the video. It is more flexible and DRY.

I hope that is somewhat helpful. Best of luck!

Gabbie Metheny
Gabbie Metheny
33,778 Points

I posted this solution here as well, but if you want to stick with setHeader(), you can pop your parameters in an array and use the ES6 spread operator to pull out the values inside `setHeader(). Works perfectly!

Christopher Woodward
Christopher Woodward
6,840 Points

I came across this same issue. The problem is that the video is outdated and the server methods copied from the Node.js site have changed. They are now:

res.setHeader('Content-Type', 'text/plain');

While in the course, at the time of recording, it showed the method:

response.writeHead('Content-Type', 'text/plain');

My app worked just fine when I changed from setHeader to writeHead and added the status code, hope that helps!

Updated code:

response.writeHead(200, commonHeaders);