title: TypeError: res.writeHead is not a function chapter 6 Fullstack javascript development with MEAN
body: <p>So I'm following along with the Full Stack Javascript development offered by Sitepoint and I've ran into a problem found within chapter 6. I've set up a basic http server which should allow incoming connections.</p>
  
  <p>When I run index.js, I get a log to the console saying it is successfully running at my designated address(127.0.0.1:1337)</p>
  
  <p>As soon as I try to go to that address in my browser it fails to connect and I get this error in my terminal</p>
  
  <pre><code>TypeError: Cannot read property 'toUpperCase' of undefined
  at Server.&lt;anonymous&gt; (/Users/user/Documents/Git projects/human-resources/index.js:8:26)
  at emitTwo (events.js:106:13)
  at Server.emit (events.js:191:7)
  at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
  at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
  </code></pre>
  
  <p>It obviously has a problem with this line </p>
  
  <pre><code>req.method = req.method.toUpperCase();
  </code></pre>
  
  <p>But I don't know why, I thought maybe if I comment it out it'll work, but when I do that I get a similar error to the above but this time says res.writeHead is not a function</p>
  
  <pre><code>TypeError: res.writeHead is not a function
  at Server.&lt;anonymous&gt; (/Users/user/Documents/Git projects/human-resources/index.js:12:9)
  at emitTwo (events.js:106:13)
  at Server.emit (events.js:191:7)
  at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:543:12)
  at HTTPParser.parserOnHeadersComplete (_http_common.js:105:23)
  </code></pre>
  
  <p>If anyone could offer some insight into where I or my code is going wrong that would be great my full code is listed below..</p>
  
  <pre><code>var http = require('http');
  
  http.createServer(function (res,req) {
    //A parsed url to work with in case there are parameters
    var _url;
  
    //In case the client uses lower case for methods
    req.method = req.method.toUpperCase();
    console.log(req.method + ' ' + req.url);
  
    if (req.method !== 'GET') {
      res.writeHead(501, {
        'Content-Type': 'text/plain'
      });
  
      return res.end(req.method + ' is not implemented by this server');
    }
  
    if (_url = /^\/employees$/i.exec(req.url)) {
      //return a list of employees
      res.writeHead(200);
      return res.end('employee list');
    } else if (_url = /^\/employees\/(\d+)$/i.exec(req.url)) {
      //find employee by id in the route
      res.writeHead(200);
      return res.end('a single employee');
    } else {
      //try to send the static file
      res.writeHead(200);
      return res.end('static file maybe');
    }
  
  }).listen(1337, '127.0.0.1');
  
  console.log('Sever Running at http://127.0.0.1:1337/');
  </code></pre>