var http = require('http');
var url = require('url');

var plugin = require('./plugin.js').handle;
var middleware = require('./middleware.js').handle;

var server = {
	start: function(config) {
		if(config)
			this.configure(config);

		// Initialize plugins and start the server
		if(Object.keys(this.plugins).length > 0) {
			this.log.debug('Initializing plugins.');
			plugin.call(this, function() {
				this.log.debug('All plugins initialized.');
				server.listen.call(this);
			});
		}
		else
			server.listen.call(this);
		return this;
	},

	listen: function() {
		this.log.info('Starting server at http://' + this.config.host + ":" + this.config.port)

		var self = this;
		this.server = http.createServer(function(request, response) {

			// Find a matching route
			var route, match;
			for(var i=0;i<self.routes.length;i++) {
				match = self.routes[i].re.exec(url.parse(request.url).pathname);
				if(match) {
					if(self.routes[i].methods.indexOf(request.method)) {
						if(self.routes[i].methods.length > 0)
							continue;
					}
					route = self.routes[i];
					break;
				}
			}

			self.log.info('[%s] %s', request.method, url.parse(request.url).pathname)

			middleware.call(self, route, request, response, function(err) {
				if(err) {
					response.status(500);
					if(self.config.env == 'development')
						response.send(err.stack);
					else
						response.send(err.toString());
				} else {
					if(route) {
						if(route.keys.length == 0)
							request.params = match;
						else {
							request.params = {}
							for(var i=0;i<route.keys.length;i++)
								request.params[route.keys[i].name] = match[i+1];
						}

						if(route.handler)
							route.handler(request, response);
					}
					else {
						response.status(404).send('404');
					}
				}
			});
		});
		this.server.listen(this.config.port, this.config.host);
	}
}

module.exports = server.start
@@ -826,24 +826,25 @@
 ngth;i++) %7B%0A
+%0A
 %09%09%09%09match = 
@@ -862,15 +862,13 @@
 %5Bi%5D.
-re.exec
+match
 (url
@@ -895,16 +895,32 @@
 pathname
+, request.method
 );%0A%09%09%09%09i
@@ -939,128 +939,51 @@
 %09%09%09%09
-if(self.routes%5Bi%5D.methods.indexOf(request.method)) %7B%0A%09%09%09%09%09%09if(self.routes%5Bi%5D.methods.length %3E 0)%0A%09%09%09%09%09%09%09continue;%0A%09%09%09%09%09%7D
+// Match found, no need to loop any further
 %0A%09%09%09
@@ -992,30 +992,27 @@
 route = 
-self
+match
 .route
-s%5Bi%5D
 ;%0A%09%09%09%09%09b
@@ -1366,224 +1366,95 @@
 oute
-) %7B%0A%09%09%09%09%09%09if(route.keys.length == 0)%0A%09%09%09%09%09%09%09request.params = match;%0A%09%09%09%09%09%09else %7B%0A%09%09%09%09%09%09%09request.params = %7B%7D%0A%09%09%09%09%09%09%09for(var i=0;i%3Croute.keys.length;i++)%0A%09%09%09%09%09%09%09%09request.params%5Broute.keys%5Bi%5D.name%5D = match%5Bi+1%5D;%0A%09%09%09%09%09%09%7D
+ && match) %7B%0A%0A%09%09%09%09%09%09request.params = match.params;%0A%09%09%09%09%09%09request.splats = match.splats;
 %0A%0A%09%09
@@ -1474,16 +1474,18 @@
 handler)
+ %7B
 %0A%09%09%09%09%09%09%09
@@ -1518,16 +1518,24 @@
 ponse);%0A
+%09%09%09%09%09%09%7D%0A
 %09%09%09%09%09%7D%0A%09

Use route's matching logic rather than implement it inside listen function.