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

Eric Thompson
Eric Thompson
9,858 Points

var commonHeaders not working for me

This question is in relation to the "Sending Content Type Headers in Node.js" video.

I am using the Node.js v6.3.0 Documentation in regards to an example of a web server. In the video, Andrew is using the:

response.writeHead(200, {'Content-Type': 'text/html'});

whereas I am using the current:

response.setHeader('Content-Type', 'text/html');

Could someone please explain why var commonHeaders = ('Content-Type', 'text/html'); does not work in this case?

For instance, I get a Workspaces error when I try to use response.setHeader(commonHeaders):

var commonHeaders = ('Content-Type', 'text/html')

//Handle HTTP route GET / and POST / i.e. Home
function home(request, response) {
  //if url == "/" && GET
  if(request.url === "/") {
    //show search
    response.statusCode = 200;
    response.setHeader(commonHeaders);
    renderer.view("header", {}, response);
    renderer.view("search", {}, response);
    renderer.view("footer", {}, response);
    response.end();
  }

with the following error message:

  Server running at http://3000/                                                                                                                                                                                 
  _http_outgoing.js:339                                                                                                                                                                                          
      throw new TypeError(                                                                                                                                                                                       
      ^                                                                                                                                                                                                          

TypeError: Header name must be a valid HTTP Token ["text/html"]

But the HTML files render if I do not create a variable, and instead use the response.setHeader('Content-Type', 'text/html');

  //Handle HTTP route GET / and POST / i.e. Home
  function home(request, response) {
    //if url == "/" && GET
    if(request.url === "/") {
      //show search
      response.statusCode = 200;
      response.setHeader('Content-Type', 'text/html');
      renderer.view("header", {}, response);
      renderer.view("search", {}, response);
      renderer.view("footer", {}, response);
      response.end();
  }

Can anyone explain what is happening here?

Jean Paul Giraldo
Jean Paul Giraldo
15,701 Points

Wow, no one answered. I have the same exact situation. How did you solve it?

Count me in as another wrestling with this one. Haven't found a resolution other than de-DRYing and leaving out the commonHeaders variable code.