title: response written to page only on second request
body: <pre><code>var http = require('http');
  var fs = require('fs');
  var path = process.argv[2];
  var str="";
  function onRequest(request, response) {
      str += "";
      console.log("Request received" + path);
      fs.readdir(path, function(err, items) {
          str += items;
      });
      response.writeHead(200, {"Context-Type": "text/plain"});
      response.write(new Buffer(str).toString());
      response.end();
  }
  http.createServer(onRequest).listen(8000);
  </code></pre>
  
  <p>The above snippet creates a http server which gets a directory path from the user as argument. Make a http request to get the list of files available in the directory and send them back as a response. </p>
  
  <p>The response is written to the page only on the second request. The page is shown empty on the first request.</p>
  
  <p>Could anyone help. Thanks in advance!!</p>