title: URL routing in Node.js
body: <p>Homework done:</p>
  
  <p><a href="http://www.nodebeginner.org" rel="nofollow noreferrer">The Node Beginner Book</a></p>
  
  <p><a href="https://stackoverflow.com/questions/2353818/how-do-i-get-started-with-node-js">How do I get started with Node.js [closed]</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8143762/routing-requests-and-structuring-response-handlers-in-node">Structuring handlers in Node</a></p>
  
  <p>Backstory: I wanted to try and write my own framework but I'm running into some troubles, most likely due to not understanding it fully.</p>
  
  <p>What I want to achieve is a syntax that looks like this:</p>
  
  <pre><code>var app = require('./app'); //this part is understood and it works in my current code.
  app.get('/someUrl', function(){ //do stuff here });
  app.post('/someOtherUrl', function(){ //do stuff here });
  </code></pre>
  
  <p>I know of the Express-framework that has this same syntax but reading their source code still eludes me.</p>
  
  <p>This might be a trivial task to achieve but I simply can't produce it, yet.</p>
  
  <p>Trying to <code>require('./app');</code> in a file deeper in the application produces a undefined object, so I'm guessing that a server is a singleton object.</p>
  
  <p>So what have I tried?</p>
  
  <p>My current code looks like this, and somehow I feel like this is the way to go, but I can't apparently do it like this.</p>
  
  <p>I'm omitting all the <code>require();</code> statements to keep it more readable.</p>
  
  <p><strong>server.js:</strong></p>
  
  <pre><code>var app = module.exports = {
      preProcess: function onRequest(request, response){
          processor.preRequest(request); //this object adds methods on the request object
          var path = urllib.parse(request.url).pathname;
          router.route(urls, path, request, response);
      },
      createServer: function() {
          console.log("Server start up done.");
          return this.server = http.createServer(this.preProcess);
      }
  };
  
  exports.app = app;
  </code></pre>
  
  <p>At the time of writing I'm experimenting with extending this object with a <code>get()</code> method.</p>
  
  <p><strong>index.js:</strong></p>
  
  <pre><code>var app = require('./server');
  app.createServer().listen('1337');
  </code></pre>
  
  <p>the <code>router.route()</code> bit basically sends the request onward into the application and inside the router.js-file I do some magic and route the request onward to a function that maps (so far) to the /urlThatWasRequested</p>
  
  <p>This is the behavior I'd like to leave behind.
  I know this might be a pretty tall order but all my code is easily discardable and I'm not afraid of rewriting the entire codebase as this is my own project.</p>
  
  <p>I hope this is sufficient in explaining my question otherwise, please say what I should add to make this a bit more clear.</p>
  
  <p>Thanks in advance!</p>