Skip to content

[WIP] Release/0.1.2 #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -62,4 +62,4 @@ methods.forEach(function(method){
return this;
};
});
});
});
46 changes: 24 additions & 22 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,25 @@
{
"name": "express-namespace",
"description": "Express namespaced routes extension",
"version": "0.1.1",
"author": "TJ Holowaychuk <[email protected]>",
"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"
}
}
"name": "express-namespace",
"description": "Express namespaced routes extension",
"version": "0.1.1",
"author": "TJ Holowaychuk <[email protected]>",
"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"
}
}
99 changes: 96 additions & 3 deletions test/namespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
});
})
Expand Down Expand Up @@ -106,4 +106,97 @@ describe('app.namespace(path, fn)', function(){
.get('/forum/23')
.expect('23', done);
})
})

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);

});
})

})