title: Node.js HTTP - TypeError: Header name must be a valid HTTP Token
body: <p>when I'm trying to set the header in the following way ,it's working absolutely fine. </p>
  
  <pre><code>response.setHeader('Content-Type', 'application/json');
  </code></pre>
  
  <p>but when I'm trying to add variable instead of exact header name/value in the following way, it's showing error :-(</p>
  
  <pre><code>response.setHeader(result.headers);
  </code></pre>
  
  <p>if you console.log("headers -> " + result.header) the result would be the same.</p>
  
  <pre><code>headers -&gt; 'Content-Type', 'application/json'
  </code></pre>
  
  <p>following are the exact error I'm getting , not able to figure out how to get around it.</p>
  
  <pre><code> _http_outgoing.js:487
  
  throw new TypeError(`Header name must be a valid HTTP Token ["${name}"]`);
  
  ^
  
  
  
   TypeError: Header name must be a valid HTTP Token ["'Content-Type', 'application/json'"]
  
  at validateHeader (_http_outgoing.js:487:11)
  
  at ServerResponse.setHeader (_http_outgoing.js:498:3)
  
  at C:\service-mocker\src\main\app.js:54:22
  
  at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:511:3)
  </code></pre>
  
  <p>Below is whole code I'm trying to implement :</p>
  
  <pre><code>var http = require('http');
  
  var fs = require('fs');
  
  var path = require('path');
  
  var repl = require('repl');
  
  var map={};
  
  var key;
  
  var value;
  
  
  
  //create a server object:
  
  var server = http.createServer(function(request, response) {
  
  
  
      fs.readFile("./resources/FileData1.txt", function(err, data) {
  
          if(err) throw err;
  
          content = data.toString().split(/(?:\r\n|\r|\n)/g).map(function(line){
  
              return line.trim();
  
          }).filter(Boolean)
  
          var result = processFile(content);
  
          console.log("url -&gt; " + result.url);
  
         console.log("status -&gt; " + result.status);
  
         console.log("headers -&gt; " + result.headers);
  
         console.log("body -&gt; " + result.body);
  
  
  
         function objToString (obj) {
  
          var str = '';
  
          for (var p in obj) {
  
              if (obj.hasOwnProperty(p)) {
  
                  str +=  obj[p] + '\n';
  
              }
  
          }
  
          return str;
  
      }
  
      function processFile(nodes) {
  
  
  
          nodes.forEach(function(node) {
  
              if(node.startsWith("//")){
  
                  key = node.substring(2, node.length-2).toLowerCase().trim();
  
                  return;    
  
              }
  
              else{
  
                  value = node;
  
              }
  
              // map[key] = value;
  
              if(key in map){
  
                  map[key].push(value);
  
              }else{
  
                  map[key]= [value]; 
  
              }
  
          });
  
  
  
          return map;
  
          // console.log(map);
  
      }
  
     if(request.url == result.url ){
  
          response.setHeader(result.headers);
  
          // response.setHeader('Content-Type', 'application/json');
  
          response.write( objToString(result.body) );
  
          response.statuscode = parseInt( result.status );
  
          response.end();
  
      }else {
  
          // response.writeHead(404, {"Content-Type": "text/html"});
  
          response.end("No Page Found");
  
          }
  
        });
  
   });
  
  
  var port = process.env.PORT || 8080;
  
  server.listen(port, function() {
  
      console.log('Listening on port ' + port);
  
  });
  </code></pre>