Skip to content

Commit

Permalink
Changed routing model, removed route logic from controllers. Changed …
Browse files Browse the repository at this point in the history
…controller template
  • Loading branch information
cliftonc committed Mar 11, 2011
1 parent a208dfd commit 35941cb
Show file tree
Hide file tree
Showing 7 changed files with 273 additions and 179 deletions.
10 changes: 7 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ function bootApplication(app) {
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'helloworld' }));
app.use(express.static(path + '/public')); // Before router to enable dynamic routing
app.use(app.router);
app.use(express.static(path + '/public'));

// Example 500 page
app.error(function(err, req, res){
Expand Down Expand Up @@ -104,10 +104,14 @@ function bootControllers(app) {
fs.readdir(path + '/controllers', function(err, files){
if (err) throw err;
files.forEach(function(file){
bootController(app, file);
// bootController(app, file);
});


});

require(path + '/controllers/AppController')(app); // Include

}

// simplistic model support
Expand All @@ -126,6 +130,6 @@ function bootController(app, file) {
template = name.replace('Controller','').toLowerCase(); // template folder for html - remove the ...Controller part.

// Include the controller
require(controller)(app,template); // Include
// require(controller)(app,template); // Include

}
112 changes: 100 additions & 12 deletions controllers/AppController.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,104 @@
var fs = require('fs');
var fs = require('fs')
, inflection = require('../lib/inflection');

var ViewTemplatePath;

module.exports = function(app,templatePath) {
var prefix = "/";
app.get(prefix, index);
ViewTemplatePath = templatePath;
module.exports = function(app) {

// app.get("/favicon.ico", function() {}); // Required if you delete the favicon.ico from public

// Plural
app.get("/:controller?", router); // Index
app.get("/:controller.:format?", router); // Index
app.get("/:controller/:from-:to.:format?", router); // Index

// Plural Create & Delete
app.post("/:controller", router); // Create
app.del("/:controller", router); // Delete all

// Singular - different variable to clarify routing
app.get("/:controller/:id.:format?", router); // To support controller/index
app.get("/:controller/:id/:action", router); // Show edit
app.put("/:controller/:id", router); // Update
app.del("/:controller/:id", router); // Delete

}

// /
function index(req, res) {

///
function router(req, res, next) {

var controller = req.params.controller ? req.params.controller : '';
var action = req.params.action ? req.params.action : '';
var id = req.params.id ? req.params.id : '';
var method = req.method.toLowerCase();
var fn = 'index';

// Default route
if(controller.length == 0) {
index(req,res,next);
return;
}

// Determine the function to call based on controller / model and method
if(id.length == 0) {

// We are plural
switch(method) {
case 'get':
fn = 'index';
break;
case 'post':
fn = 'create';
break;
case 'delete':
fn = 'destroyAll';
break;
}

} else {

// Controller name is now singular, need to switch it back
controller = controller.pluralize();

switch(method) {
case 'get':
if(action.length > 0) {
fn = action;
} else {
fn = 'show';
}
break;
case 'put':
fn = 'update';
break;
case 'delete':
fn = 'destroy';
break;
}

}

var controllerLibrary = require('./' + controller.capitalize() + 'Controller');
if(typeof controllerLibrary[fn] === 'function') {
controllerLibrary[fn](req,res,next);
} else {
res.render('404');
}

};


/**
* Default Application index - shows a list of the controllers.
* Redirect here if you prefer another controller to be your index.
* @param req
* @param res
*/
function index(req, res, next) {

/**
* If you want to redirect to another controller, uncomment
*/
// res.redirect('/controllerName');

var controllers = [];

fs.readdir(__dirname + '/', function(err, files){
Expand All @@ -23,9 +111,9 @@ function index(req, res) {
}
});

res.render(ViewTemplatePath,{controllers:controllers});
res.render('app',{controllers:controllers});

});


};
};
12 changes: 8 additions & 4 deletions eb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* Framework version.
*/

var version = '0.1.0';
var version = '0.1.1';


/**
Expand Down Expand Up @@ -62,13 +62,13 @@ function runLauncher(appLauncher) {
// Check if this is a bootstrap application?
if(isLibrary()) {
console.log('\x1b[1mDo not run this from the bootstrap library folder, please run from an empty directory or a bootstrapped app, or please run "npm install"\x1b[0m\r\n');
return;
//return;
}

// Check if this is a bootstrap application?
if(!isBootstrap() && appLauncher.command != 'create-app') {
console.log('\x1b[1mApplication not bootstrapped - you must run:\x1b[0m eb create-app\r\n');
return;
//return;
}

switch(appLauncher.command) {
Expand Down Expand Up @@ -203,7 +203,11 @@ function createApplicationAt(path) {
mkdir(path + '/public',function() {
copy(bootstrapPath + '/public/*',path + '/public');
});
mkdir(path + '/lib');

mkdir(path + '/lib',function() {
copy(bootstrapPath + '/lib/*',path + '/lib');
});

mkdir(path + '/logs');
mkdir(path + '/pids');

Expand Down
1 change: 0 additions & 1 deletion models/README
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
Models go into this folder
Empty file added public/favicon.ico
Empty file.
Loading

0 comments on commit 35941cb

Please sign in to comment.