Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

controller class

setup method
  • Loading branch information...
commit cc576d712443819d78fc574f8f76c084c36f1f9b 1 parent 980789f
@moshest moshest authored
View
30 .gitignore
@@ -1,15 +1,15 @@
-lib-cov
-*.seed
-*.log
-*.csv
-*.dat
-*.out
-*.pid
-*.gz
-
-pids
-logs
-results
-
-node_modules
-npm-debug.log
+lib-cov
+*.seed
+*.log
+*.csv
+*.dat
+*.out
+*.pid
+*.gz
+
+pids
+logs
+results
+
+node_modules
+nbproject
View
3  index.js
@@ -1,2 +1,5 @@
var express = require('express');
+module.exports.Controller = require('./lib/controller');
+
+module.exports.setup = require('./lib/setup');
View
187 lib/controller.js
@@ -0,0 +1,187 @@
+
+/**
+ * Expose Controller class
+ */
+module.exports = Controller;
+
+// Holds any controller instance
+var _controllers = [],
+
+Controller = function(url, viewPath) {
+ this._prefixUrl = url || '';
+ this._viewPath = viewPath || this._prefixUrl.substr(1);
+
+ this._mapping = {
+ get: {},
+ post: {},
+ put: {},
+ 'delete': {}
+ };
+
+ this._tags = {};
+
+ this._inits = [];
+ this._befores = [];
+ this._afters = [];
+ this._params = [];
+
+ _controllers.push(this);
+};
+
+Controller.forEach = function(fn) {
+ _controllers.forEach(fn);
+};
+
+Controller.prototype.url = function(url, req) {
+ url = this._prefixUrl + (url || '/');
+
+ if(req && req.route && req.route.keys.length) {
+
+ req.route.keys.forEach(function(key) {
+ if(!req.route.params[key.name])
+ return;
+
+ url = url.replace(new RegExp(':' + key.name + '([?/]|$)'), function(match, p1) {
+ return req.route.params[key.name] + p1;
+ });
+ });
+ }
+
+ return url;
+};
+
+Controller.prototype.view = function(path) {
+ return this._viewPath + (path || '');
+};
+
+Controller.prototype.get = function(url, fn) {
+ this.route('get', url, Array.prototype.slice.call(arguments, 1));
+ return this;
+};
+
+Controller.prototype.post = function(url, fn) {
+ this.route('post', url, Array.prototype.slice.call(arguments, 1));
+ return this;
+};
+
+Controller.prototype.put = function(url, fn) {
+ this.route('put', url, Array.prototype.slice.call(arguments, 1));
+ return this;
+};
+
+Controller.prototype.del =
+Controller.prototype['delete'] = function(url, fn) {
+ this.route('delete', url, Array.prototype.slice.call(arguments, 1));
+ return this;
+};
+
+Controller.prototype.route = function(method, url, fns) {
+ if(!this._mapping[method])
+ throw new Error('Invalid routing method');
+
+ this._mapping[method][url] = fns;
+ return this;
+};
+
+Controller.prototype.init = function(fn) {
+ this._inits.push(fn);
+ return this;
+};
+
+Controller.prototype.before = function(fn) {
+ this._befores.push(fn);
+ return this;
+};
+
+Controller.prototype.after = function(fn) {
+ this._afters.push(fn);
+ return this;
+};
+
+Controller.prototype.param = function(name, fn) {
+ this._params.push(arguments);
+ return this;
+};
+
+Controller.prototype.tag = function(name, fn) {
+ var self = this,
+ fns = Array.prototype.slice.call(arguments, 1);
+
+ if(typeof name !== 'object')
+ name = [name];
+
+ name.forEach(function(tag) {
+ if(typeof self._tags[tag] === 'undefined')
+ self._tags[tag] = [];
+
+ self._tags[tag].push.apply(self._tags[tag], fns);
+ });
+ return this;
+};
+
+Controller.prototype.middle = function() {
+ var self = this;
+
+ return function(req, res, next) {
+ res.locals.controller = {
+ req: req,
+ view: function() {
+ return self.view.apply(self, arguments);
+ },
+ url: function(url) {
+ return self.url.call(self, url, req);
+ }
+ };
+
+ return next();
+ };
+};
+
+Controller.prototype.setup = function(app) {
+ var self = this,
+ prefixUrl = this._prefixUrl,
+ middle = self.middle();
+
+ this.app = app;
+
+ for(var method in this._mapping) {
+ for(var url in this._mapping[method]) {
+ var fns = this._mapping[method][url],
+ isBeforeAfter = fns[0],
+ params = [prefixUrl + url, middle];
+
+ if(!isBeforeAfter)
+ fns = fns.slice(1);
+
+ if(isBeforeAfter)
+ params = params.concat(this._befores);
+
+ // append callbacks
+ fns.forEach(function(fn) {
+ if(typeof fn !== 'string') {
+ params.push(fn);
+ } else {
+ if(!self._tags[fn])
+ return;
+
+ params = params.concat(self._tags[fn]);
+ }
+ });
+
+ if(isBeforeAfter)
+ params = params.concat(this._afters);
+
+ app[method].apply(app, params);
+ }
+ }
+
+ this._inits.forEach(function(fn) {
+ fn.apply(self, [app]);
+ });
+
+ this._params.forEach(function(args) {
+ app.param.apply(app, args);
+ });
+
+ return this;
+};
View
20 lib/setup.js
@@ -0,0 +1,20 @@
+var Controller = require('./controller');
+
+/**
+ * Expose setup method
+ */
+module.exports = setup;
+
+
+var setup = function(options) {
+ if(options.app) {
+ setupApp(options.app);
+ }
+},
+
+setupApp = function(app) {
+ Controller.forEach(function(controller) {
+ controller.setup(app);
+ });
+};
+
View
1  package.json
@@ -10,7 +10,6 @@
"start": "node app"
},
"dependencies": {
- "express": "~3.0.0"
},
"devDependencies": {
},
View
67 readme.md
@@ -1,8 +1,73 @@
further.js
-================================
+==========
A lightweight library on Node.js
+Install
+=======
+
+ $ npm install further
+
+Simple Usage
+=====
+
+```js
+var further = require('further'),
+ app = express();
+
+// Route "GET /"
+app.get('/', function(req, res){
+ res.send('Hello World');
+});
+
+// Route "POST /submit"
+app.post('/submit', function(req, res) {
+ res.send(req.body);
+});
+
+// Create an example controller
+var controller = new further.Controller('/admin');
+
+// Add a middleware before each controller request
+controller.before(function(req, res, next) {
+ req.user = 'UserObject';
+ next();
+});
+
+// Define a routing tag
+controller.tag('requireLogin', function(req, res, next) {
+ if(!req.user) {
+ next(new Error("Login Required!");
+ return;
+ }
+
+ next();
+});
+
+// route "GET admin/" and require login!
+controller.get('/', 'requireLogin', function(req, res) {
+ res.send('Hello Admin');
+});
+
+// route "GET admin/login"
+controller.get('/login', function(req, res) {
+ res.send('Please Login');
+});
+
+// route "POST admin/login"
+controller.post('/login', function(req, res) {
+ res.send(req.body);
+});
+
+// Bind the controller routes
+further.setup({
+ app: app
+});
+
+// Start the application
+app.listen(3000);
+```
+
Changelog
=========
Please sign in to comment.
Something went wrong with that request. Please try again.