Permalink
Browse files

Merge pull request #16 from Tuskan360/patch-1

Route Middleware - Implementation
  • Loading branch information...
2 parents de62676 + 05e41aa commit c0d2c4bda6f69ed3b209816194a2c2c98da0cd3c @tj tj committed Apr 12, 2012
Showing with 57 additions and 2 deletions.
  1. +10 −1 index.js
  2. +47 −1 test/namespace.test.js
View
@@ -23,8 +23,17 @@ var express = require('express')
* @api public
*/
-exports.namespace = function(path, fn){
+exports.namespace = function(){
+ var args = Array.prototype.slice.call(arguments)
+ , path = args.shift()
+ , fn = args.pop()
+ , self = this;
(this._ns = this._ns || []).push(path);
+
+ if(args.length){
+ self.all('/*', args);
+ }
+
fn.call(this);
this._ns.pop();
return this;
View
@@ -1,4 +1,3 @@
-
/**
* Module dependencies.
*/
@@ -143,5 +142,52 @@ module.exports = {
assert.response(app,
{ url: '/user/12' },
{ body: 'OK' });
+ },
+ 'test app.namespace(str, middleware, fn)': function(done){
+ var app = express.createServer(),
+ calledA = 0,
+ calledB = 0;
+
+ function middlewareA(req,res,next){
+ calledA++;
+ next();
+ }
+ function middlewareB(req,res,next){
+ calledB++;
+ next();
+ }
+ app.namespace('/user/:id', middlewareA, function(){
+ app.get('/', function(req,res){
+ res.send('got Home');
+ });
+ app.get('/other', function(req,res){
+ res.send('got Other');
+ });
+ app.namespace('/nest', middlewareB, function(req,res){
+ app.get('/', function(req,res){
+ res.send('got Nest');
+ });
+ });
+ });
+ var pending = 3;
+ function finished() {
+ --pending || function(){
+ assert.equal(3, calledA);
+ assert.equal(1, calledB);
+ done();
+ }();
+ }
+ assert.response(app,
+ { url: '/user/12' },
+ { body: 'got Home' },
+ finished);
+ assert.response(app,
+ { url: '/user/12/other' },
+ { body: 'got Other' },
+ finished);
+ assert.response(app,
+ { url: '/user/12/nest' },
+ { body: 'got Nest' },
+ finished);
}
};

1 comment on commit c0d2c4b

sadjow commented on c0d2c4b Apr 12, 2012

When we have a tag 0.0.5 with this feature?

Please sign in to comment.