Skip to content

Instantly share code, notes, and snippets.

@calebhearth
Created March 11, 2012 00:02
Show Gist options
  • Save calebhearth/2014123 to your computer and use it in GitHub Desktop.
Save calebhearth/2014123 to your computer and use it in GitHub Desktop.
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