Created
March 11, 2012 00:02
-
-
Save calebhearth/2014123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var controllersPrefix = './controllers/'; | |
var notFoundPath = controllersPrefix + '404'; | |
function getController(resource) { | |
var controllerPath = controllersPrefix + resource; | |
var controller; | |
if path.existsSync(controllerPath) { | |
controller = require(controllerPath); | |
} | |
else { | |
controller = require(notFoundPath); | |
} | |
controller = require('./controllers/' + resource); | |
return controller; | |
} | |
function route(url) { | |
if (url == '/') { getController('index').handle(); } | |
else { | |
parts = url.split('/'); | |
// first part is the resource name | |
resource = parts[0]; | |
// the rest is for the controller to deal with | |
queryParts = parts.slice(1); | |
console.log(resource, querystring); | |
getController(resource).handle(querystring); | |
} | |
} | |
exports.route = route; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment