Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add route middleware and bump version

Change-Id: Ic8dff660da837e3ea0f17001e2e83fcac942cee2
  • Loading branch information...
commit 14c347cec18f2061a3c7997097e8f6bf43d8d698 1 parent 102ddb1
Tim Caswell authored
Showing with 28 additions and 54 deletions.
  1. +0 −53 controllers.js
  2. +1 −1  package.json
  3. +27 −0 route.js
View
53 controllers.js
@@ -1,53 +0,0 @@
-var Url = require('url'),
- Fs = require('fs'),
- Path = require('path');
-
-// MVC style controller routing
-module.exports = function (root, controllerFolder) {
-
- // Normalize mount points to always end in /
- if (root[root.length - 1] !== "/") { root += "/"; }
- // Normalize controller folder so require understands it
- controllerFolder = Fs.realpathSync(controllerFolder);
-
- // Load the controllers at startup
- var controllers = {};
- Fs.readdirSync(controllerFolder).forEach(function (name) {
- var pos = name.lastIndexOf('.');
- var ext = name.substr(pos + 1);
- if (ext === 'js') {
- controllers[name.substr(0, pos)] = require(controllerFolder + "/" + name);
- }
- });
-
- // Generate a request handling function
- return function (req, res, next) {
- // parse out pathname if it's not there already (other middleware may have done it already)
- if (!req.hasOwnProperty('uri')) { req.uri = Url.parse(req.url); }
-
- // Mount relative to the given root
- var path = req.uri.pathname;
- if (path.substr(0, root.length) !== root) { return next(); }
-
- // Get the requested controller and method
- var parts = path.substr(root.length).split('/');
- if (parts.length === 0) { parts[0] = "index"; } // Default module to "index"
- if (parts.length === 1) { parts[1] = "index"; } // Default method to "index"
-
- // Find the controller
- var controller = parts.shift();
- if (!controllers.hasOwnProperty(controller)) { return next(); }
- controller = controllers[controller];
-
- // Find the method
- var method = parts.shift();
- if (!controller.hasOwnProperty(method)) { return next(); }
-
- // Call it!
- var args = [req, res, next];
- args.push.apply(args, parts);
- controller[method].apply(controller, args);
- };
-
-};
-
View
2  package.json
@@ -2,7 +2,7 @@
"name": "creationix",
"description": "Creationix is a meta package for my personal packages",
"tags": ["stack", "creationix", "meta", "sandbox"],
- "version": "0.1.4",
+ "version": "0.1.5",
"author": "Tim Caswell <tim@creationix.com>",
"repository": {
"type": "git",
View
27 route.js
@@ -0,0 +1,27 @@
+var Url = require("url");
+
+module.exports = function setup(method, route, handler) {
+ var names = [];
+ var compiled = route.replace(/:[a-z$_][a-z0-9$_]*.?/gi, function (match) {
+ if ((/[^a-z$_0-9]$/i).test(match)) {
+ var end = match.substr(match.length - 1);
+ names.push(match.substr(1, match.length - 2));
+ return "([^" + end + "]+)" + end;
+ }
+ names.push(match.substr(1));
+ return "(.*)";
+ });
+ compiled = "^" + compiled + "$";
+ var regexp = new RegExp(compiled);
+ return function (req, res, next) {
+ if (req.method !== method) return next();
+ if (!req.hasOwnProperty("uri")) { req.uri = Url.parse(req.url); }
+ var match = req.uri.pathname.match(regexp);
+ if (!match) return next();
+ var params = {};
+ Array.prototype.slice.call(match, 1).forEach(function (value, i) {
+ params[names[i]] = value;
+ });
+ handler(req, res, params, next);
+ }
+};
Please sign in to comment.
Something went wrong with that request. Please try again.