title: Node.js with fs.readFile not able to render file content via http
body: <p>I'm learning node.js but am struggling with the <code>fs.readFile</code> function in my simple web server code (demo.js below). If I access <code>http://localhost:8001/demo.html</code> I get no data back in the browser, but if I access '/' or any other file name I see the "hello world' or 404 pages as expected. </p>
  
  <p>I have included the content of demo.html and the node console output where the log messages suggest that the readFile is reading the file OK, but I get nothing in the browser and don't see the 'Got here D' log message. </p>
  
  <p>I'm sure I must be missing something obvious but can't see what. Any help would be appreciated!</p>
  
  <p>demo.js code:</p>
  
  <pre><code>var http = require("http");
  var url = require('url');
  var fs = require('fs');
  
  var server = http.createServer(function(request, response){
      console.log('Connection');
      var path = url.parse(request.url).pathname;
  
      switch(path){
          case '/':
              response.writeHead(200, {'Content-Type': 'text/html'});
              response.write('hello world');
              break;
          case '/demo.html':
              fs.readFile(__dirname + path, function(error,data){
                  console.log("error: " + error);
                  console.log("data: " + data);
                  if (error){
                      console.log("Got here A");
                      response.writeHead(404);
                      response.write("oops problem loading this page - 404");
                  }   
                  else{
                      console.log("Got here B");
                      response.writeHead(200, {"Content-Type": "text/html"});
                      response.write(data);
                  }
              });
              break;
          default:
              console.log("Got here C");
              response.writeHead(404);
              response.write("oops this page doesn't exist - 404");
              break;
      }
      response.end();
      console.log("Got here D");
  });
  
  server.listen(8001);
  </code></pre>
  
  <p>demo.html:</p>
  
  <pre><code>&lt;html&gt;
  &lt;body&gt;This is my demo page&lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>node console output:</p>
  
  <pre><code>C:\code\Node&gt;node demo.js
  Connection
  Got here D
  error: null
  data: &lt;html&gt;
  &lt;body&gt;This is my demo page&lt;/body&gt;
  &lt;/html&gt;
  
  Got here B
  </code></pre>