title: Using express to set up a basic server
body: <p>I have a node.js application and I am stuck getting an error message when I try to load the homepage. I will do my best at laying out my architecture below. It goes index.js --> server.js --> router.js --> requestHandlers.js</p>
  
  <p>I am using a combination of express (www.expressjs.com) and nodebeginner.org. Sorry for the long question.. just wanted to get as much information down as possible.</p>
  
  <p>index.js (creates handle object that contains pathname/requesthandler information, calls function to start server) I start with router.route here and pass it along each step</p>
  
  <pre><code>var server = require("./server");
  var router = require('./router');
  var requestHandlers = require('./requestHandlers');
  
  // Routes
  
  var handle = {}
  handle['/'] = requestHandlers.home;
  
  server.start(router.route, handle)
  </code></pre>
  
  <p>server.js (starts the server, THIS IS WHERE I WANT TO CONFIGURE THE SERVER, gets a the pathname from the URL, and passes it on to the route module)</p>
  
  <pre><code>var http = require("http");
  var url = require('url');
  var express = require('express');
  
  function start (route, handle) {
      var onRequest = function(request, res) {
          var pathname = url.parse(request.url).pathname;
          console.log("request for " + pathname + " recieved.");
          route(handle, pathname, res);
      }
  
      var app = express.createServer(onRequest).listen(8888);
      if (app.configure) {
          console.log('app exists'); //logging correctly
      }
      // Configuration
       app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
  });
  
  app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
  });
  
  app.configure('production', function(){
    app.use(express.errorHandler());
  });
  
      console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env); //logs correct with 8888 and development
  }
  
  exports.start = start;
  </code></pre>
  
  <p>router.js (the function route passed from index --> server which calls the route function in router.js, calls the requestHandler that matches the pathname in handle object)</p>
  
  <pre><code>function route (handle, pathname, res) {
  
      console.log("About to route a request for" + pathname); //About to route a request for/stylesheets/style.css SEE BELOW******* this is the error
  
  if (typeof handle[pathname] === 'function') {
          handle[pathname] (res);
      }
      else {
          console.log('no request handler found for' + pathname);
      }
  }
  
  exports.route = route;
  </code></pre>
  
  <p>requestHandler.js (interacts with the res/req objects, functions are mapped to certain pathnames, only called when those pathnames are requested thanks to the router)</p>
  
  <pre><code>var home = function(res){
    res.render('index', { title: 'WWYB?' });
    console.log('homepage rendered'); //correctly logs for pathname = '/'
    //click();
  };
  
  exports.home = home;
  </code></pre>
  
  <p><em><strong></em>**</strong>*when i go to request localhost:8888 it tries to make a bunch of requests. first it requests "/" correctly but then keeps going through logging everything saying "About to route a request for/stylesheets/style.css" Eventually the page loads with no css. The pathname as indicated in my layout.jade file is exactly that '/stylesheets/style.css'.</p>
  
  <p>Why is the pathname ever evaluating to /stylesheets/style.css? I think node is doing something in the background and I dont fully understand it.</p>
  
  <p>Let me know if you need more info. Thanks!</p>