diff --git a/index.js b/index.js index 42154ae..59abf36 100644 --- a/index.js +++ b/index.js @@ -53,7 +53,7 @@ methods.forEach(function(method){ , self = this; this.namespace(path, function(){ - path = this._ns.join('/').replace(/\/\//g, '/').replace(/\/$/, '') || '/'; + path = 'string' !== typeof path ? path : this._ns.join('/').replace(/\/\//g, '/').replace(/\/$/, '') || '/'; args.forEach(function(fn){ orig.call(self, path, fn); }); @@ -62,4 +62,4 @@ methods.forEach(function(method){ return this; }; }); -}); \ No newline at end of file +}); diff --git a/package.json b/package.json index 152aa88..8a53dbd 100644 --- a/package.json +++ b/package.json @@ -1,23 +1,25 @@ { - "name": "express-namespace", - "description": "Express namespaced routes extension", - "version": "0.1.1", - "author": "TJ Holowaychuk ", - "keywords": [ - "express" - ], - "main": "index", - "dependencies": { - "methods": "0.0.1" - }, - "devDependencies": { - "express": "3.x", - "ejs": "*", - "mocha": "*", - "supertest": "*" - }, - "repository": { - "type": "git", - "url": "https://github.com/visionmedia/express-namespace.git" - } -} \ No newline at end of file + "name": "express-namespace", + "description": "Express namespaced routes extension", + "version": "0.1.1", + "author": "TJ Holowaychuk ", + "scripts": { + "test": "make test" + }, + "keywords": [ + "express" + ], + "main": "index", + "dependencies": { + "express": "^4.3.0", + "methods": "^1.0.0" + }, + "devDependencies": { + "mocha": "*", + "supertest": "*" + }, + "repository": { + "type": "git", + "url": "https://github.com/visionmedia/express-namespace.git" + } +} diff --git a/test/namespace.test.js b/test/namespace.test.js index 2aa7c98..f04feb1 100644 --- a/test/namespace.test.js +++ b/test/namespace.test.js @@ -33,7 +33,7 @@ describe('app.namespace(path, fn)', function(){ it('should prefix within .namespace()', function(done){ var app = express(); - done = pending(4, done); + done = pending(6, done); app.get('/one', function(req, res){ res.send('GET one'); @@ -49,7 +49,7 @@ describe('app.namespace(path, fn)', function(){ res.send('GET baz'); }); - app.del('/all', function(req, res){ + app.delete('/all', function(req, res){ res.send('DELETE all baz'); }); }) @@ -106,4 +106,97 @@ describe('app.namespace(path, fn)', function(){ .get('/forum/23') .expect('23', done); }) -}) \ No newline at end of file + + it('should support VERB methods as express', function(){ + var app = express(); + + app.namespace('/method', function(){ + app.all('/', function(){ }); + }); + for(var method in app.routes) { + assert.equal(app.routes[method][0].path, '/method', 'not support method ' + method); + } + }) + + + it('should not die with regexes, but they ignore namespacing', function(done){ + var app = express(); + done = pending(3, done); + + app.get(/test\d\d\d/, function(req, res) { + res.send("GET test"); + }); + + app.namespace('/forum/:id', function(){ + + app.get('/', function(req, res){ + res.send('' + req.params.id); + }); + + app.get(/^\/((?!login$|account\/login$|logout$)(.*))/, function(req, res) { + res.send("crazy reg"); + }); + + }); + + request(app) + .get('/forum/23') + .expect('23', done); + + request(app) + .get('/test123') + .expect('GET test', done); + + request(app) + .get('/account/123') + .expect('crazy reg', done); + }) + + describe('routes with regexp', function(){ + + it('should allow regexp routes', function(done){ + var app = express(); + done = pending(2, done); + + app.namespace('/forum/:id', function(){ + app.get('/((view)?)', function(req, res){ + res.send('' + req.params.id); + }); + }); + + request(app) + .get('/forum/23/') + .expect('23', done); + + request(app) + .get('/forum/23/view') + .expect('23', done); + + }) + + it('should allow for complex regexp in routes', function(done){ + var app = express(); + + done = pending(2, done); + + app.namespace('/blog/:id', function(){ + app.get('/((:page)?)', function(req, res){ + if (req.params.page){ + return res.send('' + req.params.page); + } + res.send('' + req.params.id); + }) + }); + + request(app) + .get('/blog/23/') + .expect('23', done); + + request(app) + .get('/blog/23/12') + .expect('12', done); + + }); + }) + +})