title: TypeError: Cannot call method 'writeHead' of undefined
body: <p>I am running a simple nodejs application using index, server, router and requestHandler and it shows an error as soon as the line response.writeHead(200,{"Content-Type":"text/html"}); is reached. The code is:</p>
  
  <p>File requestHandler.js:</p>
  
  <pre><code>    var querystring=require("querystring"),
      fs=require("fs"),
      formidable=require("formidable");
  
  function start(response){
      console.log("Request handler 'start' was called.");
      var body='&lt;html&gt;'+
          '&lt;head&gt;'+
          '&lt;meta http-equiv="Content-Type"'+
          'content="text/html; charset=UTF-8"/&gt;'+
          '&lt;/head&gt;'+
          '&lt;body&gt;'+
          '&lt;form action="/upload" enctype="multipart/form-data"'+
          'method="post"&gt;'+
          '&lt;input type="file" name="upload" mulitple="multiple"&gt;'+
          '&lt;input type="submit" value="Upload file"/&gt;'+
          '&lt;/form&gt;'+
          '&lt;/body&gt;'+
          '&lt;/html&gt;';
      response.writeHead(200,{"Content-Type":"text/html"});
      response.write(body);
      response.end();
  }
  </code></pre>
  
  <p>File index.js:</p>
  
  <pre><code>var server= require("./server");
  var router= require("./router");
  var requestHandlers= require("./requestHandlers");
  
  var handle={}
  handle["/"]=requestHandlers.start;
  handle["/start"]=requestHandlers.start;
  server.start(router.route, handle);
  </code></pre>
  
  <p>File router.js:</p>
  
  <pre><code>function route(handle, pathname, request, response){
      console.log("About to route request for "+pathname);
      if (typeof handle[pathname] === 'function'){
          handle[pathname](request, response);
      }else{
          console.log("No request handler found for" +pathname);
          response.writeHead(404, {"Content-Type":"text/html"});
          response.write("404 Not found");
          response.end();
      }
  }
  exports.route=route;
  </code></pre>
  
  <p>File server.js:</p>
  
  <pre><code>var http=require("http");
  var url=require("url");
  
      function start(route, handle){
          function onRequest(request, response){
              var pathname=url.parse(request.url).pathname;
              console.log("Request for " + pathname + " received");
              route(handle, pathname);
              route(handle,pathname,response,request);
          }
          http.createServer(onRequest).listen(8888);
          console.log("Server has started");
      }
  exports.start=start;
  </code></pre>