Node.js "write after end" error
<p>Starting off with a basic node app, and I can't figure out how to beat this "write after end" error, even with the callbacks suggested on several sites.</p>

<p>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;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);
</code></pre>

<p>server.js:</p>

<pre><code>var http = require("http");
var url = require("url");

function start(route, handle) {
  function onRequest(request, response) {
    var postData ="";
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(handle, pathname, response);
   }

  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}

exports.start = start;
</code></pre>

<p>router.js:</p>

<pre><code>function route(handle, pathname, response) {
  console.log("About to route a request for " + pathname);
  if (typeof handle[pathname] === 'function') {
    handle[pathname](response);
  } 
  else {
    console.log("No request handler found for " + pathname);
    response.writeHead(404, {"Content-Type": "text/plain"});
    response.write("404 Not found");
    response.end();
  }
}

exports.route = route;

and now the culprit, requestHandlers.js:

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;div&gt;Hello Start&lt;/div&gt;' +
    '&lt;/body&gt;'+
    '&lt;/html&gt;';

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(body);
    response.end();
}

function upload(response) {
    console.log("Request handler 'upload' was called");
    response.writeHead(200, {"Content-Type": "text/plain"}); 
    response.write("Hello Upload");
    response.end();
}

exports.start = start;
exports.upload = upload;
</code></pre>

<p>And here's the stack trace:</p>

<blockquote>
  <p>C02RF8L7FVH5:HelloWorld francop$ node index.js
  Server has started.
  Request for / received.
  About to route a request for /
  Request handler 'start' was called.
  About to route a request for /
  Request handler 'start' was called.
  events.js:141
        throw er; // Unhandled 'error' event
        ^</p>
  
  <p>Error: write after end
      at ServerResponse.OutgoingMessage.write (_http_outgoing.js:430:15)
      at Object.start              (/Users/francop/Documents/Apps/HelloWorld/requestHandlers.js:18:14)
      at route (/Users/francop/Documents/Apps/HelloWorld/router.js:5:21)
      at IncomingMessage.     (/Users/francop/Documents/Apps/HelloWorld/server.js:19:6)
      at emitNone (events.js:67:13)
      at IncomingMessage.emit (events.js:166:7)
      at endReadableNT (_stream_readable.js:921:12)
      at nextTickCallbackWith2Args (node.js:442:9)
      at process._tickCallback (node.js:356:17)</p>
</blockquote>

<p>The only answers I found anywhere were "It's async so use a callback" like so:</p>

<pre><code>response.write(body, function(err) {
    response.end();
});
</code></pre>

<p>...Which throws the exact same error.</p>

<p>Any help with this would be amazing. The few sparse answers I've found online don't help at all.</p>

<p>Thanks!</p>