title: Need help understanding Node.js in respect to routing using a JavaScript API?
body: <p>I understand the basics of routing in Node.js and using the http module for it. I understand all the Node.js code below but just the JavaScript API part and how it is used to make the routing code much more cleaner is what I have trouble understanding. When I say "trouble understanding" I mean trouble understanding the syntax and how the routes object is used.</p>
  
  <p>The code is from an E-book I have been learning from so please read the code below.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var http = require("http"); 
  var url = require("url");
  
  var route = {   
    routes : {},
    for: function(path, handler){    
      this.routes[path] = handler;  
    } 
  };
  
  route.for("/start", function(request, response){    
    response.writeHead(200, {"Content-Type": "text/plain"});    
    response.write("Hello");    response.end();  
  });
  
    route.for("/finish", function(request, response){    
      response.writeHead(200, {"Content-Type": "text/plain"});    
      response.write("Goodbye");   
      response.end();  
    });
  
  function onRequest(request, response) {  
    var pathname = url.parse(request.url).pathname;  
    console.log("Request for " + pathname + " received.");  
    if(typeof route.routes[pathname] ==='function'){    
      route.routes[pathname](request, response);  
    }else{    
      response.writeHead(404, {"Content-Type": "text/plain"});    
      response.end("404 Not Found");  
    } 
  }
  
  http.createServer(onRequest).listen(9999); 
  console.log("Server has started.")</code></pre>
  </div>
  </div>
  </p>
  
  <p>My understanding so far is that: <code>route.routes</code> is an empty object and <code>route.for</code> is a function. The function has two parameters <code>function(path,handler)</code> but I don't understand the part in the function i.e.  <code>this.routes[path] = handler;</code>   </p>
  
  <p>From my understanding <code>this.routes[path]</code> is an empty object so is the code setting <code>handler</code> to an empty object?</p>
  
  <p>and beyond this I have absolutely no clue what <code>function onRequest(request,response){};</code> is doing. </p>
  
  <p>Plase explain the whole code for me as I find it very disturbing not being able to understanding the basics before progressing through the E-book.</p>