title: How do I serve static files through Node.js locally?
body: <p>I have the following file locations :</p>
  
  <pre><code>file:///Users/MyName/Developer/sitename/scripts (contains all .js files..)
  file:///Users/MyName/Developer/sitename/css (contains all .css files..)
  file:///Users/MyName/Developer/sitename/images (contains all .jpg/png/etc. files..)
  file:///Users/MyName/Developer/sitename/sitename.html
  file:///Users/MyName/Developer/sitename/server.js
  </code></pre>
  
  <p>Inside <code>sitename.html</code> I load all necessary files as follows for example :</p>
  
  <pre><code>&lt;html&gt;
    &lt;head&gt;
      &lt;script src="scripts/somefile.js"&gt;&lt;/script&gt;
    &lt;/head&gt;
    ...
  &lt;/html&gt;
  </code></pre>
  
  <p>So whenever I open up <code>file:///Users/MyName/Developer/sitename/sitename.html</code> everything works fine.</p>
  
  <p>However, whenever I try to load <code>file:///Users/MyName/Developer/sitename/sitename.html</code> via a local Node.js server that I've setup (server file location : <code>file:///Users/MyName/Developer/sitename/server.js</code>) as follows :</p>
  
  <pre><code>var http = require("http"); 
  var fs = require("fs");
  
  fs.readFile('./sitename.html', function (err, html) 
  {
      if (err) throw err; 
  
      http.createServer(function (request,response)
      {  
          // serve site
          if (request.url === "/")
          {
              response.writeHeader(200, {"Content-Type": "text/html"});  
              response.write(html);  
          }
          response.end(); 
      }).listen(8080); 
  });
  </code></pre>
  
  <p><code>sitename.html</code> is found and loaded but all the other files that are supposed to load through it fail to load because they're all given the prefix <code>http://localhost:8080/</code> (<code>http://localhost:8080/scripts/somefile.js</code>is not a valid file path for example).</p>
  
  <p>It looks like as soon as the server is created (inside <code>http.createServer(.....);</code>) the context changes and the parent directory now becomes <code>http://localhost:8080/</code> instead of <code>file:///Users/MyName/Developer/sitename/</code> which makes sense I guess but is not very helpful when using files that are still stored locally.</p>
  
  <p>How do I work around that? Is the fact that I'm storing <code>server.js</code> (just for the time being) in the same directory making things even more confusing?</p>
  
  <p>Thanks!</p>