Skip to content

Commit

Permalink
Added "case sensitive routes" option.
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Apr 29, 2011
1 parent d2f963d commit d2adcbd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions docs/guide.md
Expand Up @@ -83,6 +83,7 @@ Express supports the following settings out of the box:
* _view engine_ Default view engine name for views rendered without extensions
* _view options_ An object specifying global view options
* _view cache_ Enable view caching (enabled in production)
* _case sensitive routes_ Enable case-sensitive routing

### Routing

Expand Down
2 changes: 1 addition & 1 deletion lib/http.js
Expand Up @@ -87,7 +87,7 @@ app.init = function(middleware){
if (middleware) middleware.forEach(self.use.bind(self));

// use router, expose as app.get(), etc
var fn = router(function(app){ self.routes = app; });
var fn = router(function(app){ self.routes = app; }, this);
this.__defineGetter__('router', function(){
this.__usedRouter = true;
return fn;
Expand Down
5 changes: 3 additions & 2 deletions lib/router/index.js
Expand Up @@ -34,7 +34,7 @@ exports.methods = _methods;
* @api private
*/

function router(fn){
function router(fn, app){
var self = this
, methods = {}
, routes = {}
Expand Down Expand Up @@ -84,7 +84,8 @@ function router(fn){
if (!path) throw new Error(name + ' route requires a path');
if (!fn) throw new Error(name + ' route ' + path + ' requires a callback');

var route = new Route(name, path, fn);
var options = { sensitive: app.enabled('case sensitive routes') };
var route = new Route(name, path, fn, options);
localRoutes.push(route);
return self;
};
Expand Down
17 changes: 12 additions & 5 deletions lib/router/route.js
Expand Up @@ -13,18 +13,24 @@ module.exports = Route;

/**
* Initialize `Route` with the given HTTP `method`, `path`,
* and callback `fn`.
* and callback `fn` and `options`.
*
* Options:
*
* - `sensitive` enable case-sensitive routes
*
* @param {String} method
* @param {String} path
* @param {Function} fn
* @param {Object} options.
* @api private
*/

function Route(method, path, fn) {
function Route(method, path, fn, options) {
options = options || {};
this.callback = fn;
this.path = path;
this.regexp = normalize(path, this.keys = []);
this.regexp = normalize(path, this.keys = [], options.sensitive);
this.method = method;
}

Expand All @@ -39,11 +45,12 @@ function Route(method, path, fn) {
*
* @param {String|RegExp} path
* @param {Array} keys
* @param {Boolean} sensitive
* @return {RegExp}
* @api private
*/

function normalize(path, keys) {
function normalize(path, keys, sensitive) {
if (path instanceof RegExp) return path;
path = path
.concat('/?')
Expand All @@ -60,5 +67,5 @@ function normalize(path, keys) {
})
.replace(/([\/.])/g, '\\$1')
.replace(/\*/g, '(.+)');
return new RegExp('^' + path + '$', 'i');
return new RegExp('^' + path + '$', sensitive ? '' : 'i');
}
22 changes: 22 additions & 0 deletions test/router.test.js
Expand Up @@ -241,5 +241,27 @@ module.exports = {
app.match.get('/').should.have.be.empty;
app.match.all('/user/123').should.have.length(3);
app.match('/user/123').should.have.length(3);
},

'test "case sensitive routes" setting': function(){
var app = express.createServer();

app.enable('case sensitive routes');

app.get('/account', function(req, res){
res.send('account');
});

app.get('/Account', function(req, res){
res.send('Account');
});

assert.response(app,
{ url: '/account' },
{ body: 'account' });

assert.response(app,
{ url: '/Account' },
{ body: 'Account' });
}
};

0 comments on commit d2adcbd

Please sign in to comment.