title: Node.js - external JS and CSS files (just using node.js not express)
body: <p>Im trying to learn node.js and have hit a bit of a roadblock.</p>
  
  <p>My issue is that i couldn't seem to load an external css and js file into a html file.</p>
  
  <pre><code>GET http://localhost:8080/css/style.css 404 (Not Found) 
  GET http://localhost:8080/js/script.css 404 (Not Found) 
  </code></pre>
  
  <p>(this was when all files were in the root of the app)</p>
  
  <p>I was told to somewhat mimic the following app structure, add a route for the public dir to allow the webserver to serve the external files.</p>
  
  <p>my app structure is like so</p>
  
  <pre><code>domain.com
    app/
      webserver.js
  
    public/
      chatclient.html
  
      js/
        script.js
  
      css/
        style.css
  </code></pre>
  
  <p>So my webserver.js script is in the root of app, and everything I want to access is in 'public'.</p>
  
  <p>I also saw <a href="http://www.thecodinghumanist.com/blog/archives/2011/5/6/serving-static-files-from-node-js" rel="noreferrer">this example</a> that uses path.extname() to get any files extentions located in a path. (see the last code block).</p>
  
  <p>So I've tried to combine the new site structure and this path.extname() example, to have the webserver allow access to any file in my public dir, so I can render the html file, which references the external js and css files.</p>
  
  <p>My webserver.js looks like this.</p>
  
  <pre><code>var http = require('http')
  , url = require('url')
  , fs = require('fs')
  , path = require('path')
  , server;
  
  server = http.createServer(function(req,res){
  
    var myPath = url.parse(req.url).pathname;
  
      switch(myPath){
  
        case '/public':
  
          // get the extensions of the files inside this dir (.html, .js, .css)
          var extname = mypath.extname(path);
  
            switch (extname) {
  
              // get the html
              case '.html':
                fs.readFile(__dirname + '/public/chatclient.html', function (err, data) {
                  if (err) return send404(res);
                  res.writeHead(200, {'Content-Type': 'text/html'});
                  res.write(data, 'utf8');
                  res.end();
                });
              break;
  
              // get the script that /public/chatclient.html references
              case '.js':
                fs.readFile(__dirname + '/public/js/script.js', function (err, data) {
                  if (err) return send404(res);
                  res.writeHead(200, { 'Content-Type': 'text/javascript' });
                  res.end(content, 'utf-8');
                  res.end();
                });
              break;
  
              // get the styles that /public/chatclient.html references
              case '.css':
                fs.readFile(__dirname + '/public/css/style.css', function (err, data) {
                  if (err) return send404(res);
                  res.writeHead(200, { 'Content-Type': 'text/javascript' });
                  res.end(content, 'utf-8');
                  res.end();
                });
            }
            break;
  
            default: send404(res);
          }
      });
  </code></pre>
  
  <p>Inside the case of public, I'm trying to get any of the folders/files inside of this dir via
  var extname = mypath.extname(path);
  Similar to the link I provided.</p>
  
  <p>But at the moment 'extname' is empty when I console log it.</p>
  
  <p>Can anyone advise what I might need to add or tweek here?
  I'm aware this can be done easily in Express, but I would like to know how to achieve the same thing just relying on Node.</p>
  
  <p>I's appreciate any help on this.</p>
  
  <p>Thanks in advance.</p>