Permalink
Browse files

Middleware stack

  • Loading branch information...
1 parent 34abc36 commit 4f9c09781ac349947b7ca9181aaf4f4f23de1bb4 @1602 committed Dec 21, 2011
Showing with 62 additions and 37 deletions.
  1. +10 −1 README.md
  2. +51 −35 lib/railway_routes.js
  3. +1 −1 package.json
View
@@ -6,7 +6,7 @@ This is simple routing for ExpressJS framework. It allows you to write routes in
After creating app instead of writing code like `app.get('smth', doSmth);` generate routes like that:
- var map = require('railway-routes').Map(app, handler);
+ var map = new require('railway-routes').Map(app, handler);
map.resources('posts');
map.namespace('admin', function (admin) {
admin.resources('users');
@@ -49,6 +49,7 @@ In that case your handler should be:
## Features
- resourceful routes
+- generic routes
- url helpers
- namespaces
- custom helper names / paths for resources
@@ -57,3 +58,11 @@ In that case your handler should be:
http://railwayjs.com/routing.html
+## Example app
+
+Check out example app to deal with middleware, route handling, and generic routes:
+
+ git clone git://github.com/anatoliychakkaev/railway-routes-example-app.git
+ cd railway-routes-example-app
+ npm install
+ node app.js
View
@@ -11,6 +11,7 @@ function Map(app, bridge) {
this.globPath = '/';
this.pathTo = {};
this.dump = [];
+ this.middlewareStack = [];
}
Map.prototype.urlHelperName = function (path, action) {
@@ -84,7 +85,7 @@ Map.prototype.urlHelperName = function (path, action) {
var args = [path];
if (middleware) {
- args = args.concat(middleware);
+ args = args.concat(this.middlewareStack.concat(middleware));
}
args = args.concat(this.bridge(this.ns, controller, action));
@@ -177,42 +178,44 @@ Map.prototype.resources = function (name, params, actions) {
}
// now let's walk through action routes
for (var action in activeRoutes) {
- var route = activeRoutes[action].split(/\s+/);
- var method = route[0];
- var path = route[1];
-
- // append format
- if (path == '/') {
- path = '.:format?';
- } else {
- path += '.:format?';
- }
-
- // middleware logic (backward compatibility)
- var middlewareExcept = params.middlewareExcept, skipMiddleware = false;
- if (middlewareExcept) {
- if (typeof middlewareExcept == 'string') {
- middlewareExcept = [middlewareExcept];
+ (function (action) {
+ var route = activeRoutes[action].split(/\s+/);
+ var method = route[0];
+ var path = route[1];
+
+ // append format
+ if (path == '/') {
+ path = '.:format?';
+ } else {
+ path += '.:format?';
}
- middlewareExcept.forEach(function (a) {
- if (a == action) {
- skipMiddleware = true;
+
+ // middleware logic (backward compatibility)
+ var middlewareExcept = params.middlewareExcept, skipMiddleware = false;
+ if (middlewareExcept) {
+ if (typeof middlewareExcept == 'string') {
+ middlewareExcept = [middlewareExcept];
}
- });
- }
+ middlewareExcept.forEach(function (a) {
+ if (a == action) {
+ skipMiddleware = true;
+ }
+ });
+ }
- // params.path setting allows to override common path component
- var effectivePath = (params.path || name) + path;
-
- // and call map.{get|post|update|delete}
- // with the path, controller, middleware and options
- this[method.toLowerCase()].call(
- this,
- effectivePath,
- name + '#' + action,
- skipMiddleware ? [] : params.middleware,
- getParams(action, params)
- );
+ // params.path setting allows to override common path component
+ var effectivePath = (params.path || name) + path;
+
+ // and call map.{get|post|update|delete}
+ // with the path, controller, middleware and options
+ this[method.toLowerCase()].call(
+ this,
+ effectivePath,
+ name + '#' + action,
+ skipMiddleware ? [] : params.middleware,
+ getParams(action, params)
+ );
+ }.bind(this))(action);
}
// calculate set of routes based on params.only and params.except
@@ -278,13 +281,26 @@ Map.prototype.resources = function (name, params, actions) {
}
};
-Map.prototype.namespace = function (name, subroutes) {
+Map.prototype.namespace = function (name, options, subroutes) {
+ if (typeof options === 'function') {
+ subroutes = options;
+ options = null;
+ }
+ if (options && typeof options.middleware === 'function') {
+ options.middleware = [options.middleware];
+ }
// store previous ns
var old_ns = this.ns, oldGlobPath = this.globPath;
// add new ns to old (ensure tail slash present)
this.ns = old_ns + name.replace(/\/$/, '') + '/';
this.globPath = oldGlobPath + name.replace(/\/$/, '') + '/';
+ if (options && options.middleware) {
+ this.middlewareStack = this.middlewareStack.concat(options.middleware);
+ }
subroutes(this);
+ if (options && options.middleware) {
+ options.middleware.forEach([].pop.bind(this.middlewareStack));
+ }
this.ns = old_ns;
this.globPath = oldGlobPath;
};
View
@@ -9,7 +9,7 @@
"type": "git",
"url": "git://github.com/1602/railway-routes.git"
},
- "main": "lib/railway-routes.js",
+ "main": "lib/railway_routes.js",
"scripts": {
"test": "make test"
},

0 comments on commit 4f9c097

Please sign in to comment.