Compare commits

...

9 Commits

Author SHA1 Message Date
Tj Holowaychuk
1bb798d963 Release 2.3.10 2011-05-27 09:20:03 -07:00
Tj Holowaychuk
91997e9c53 Added req.route, exposing the current route. Closes #11
this can be used with a dynamicHelper to expose the _last_
route that was matched, aka the end-point when rendering a template.

this is a `Route` instance, so it has .path, .regexp, etc.
2011-05-27 09:12:49 -07:00
Tj Holowaychuk
1393187040 Merge branch 'refactor/executable' 2011-05-26 10:31:29 -07:00
Tj Holowaychuk
6e69c880d9 Added package.json generation support to express(1) 2011-05-26 10:31:21 -07:00
Tj Holowaychuk
59dcd03972 removed suggestions 2011-05-26 10:18:56 -07:00
Tj Holowaychuk
11482546a2 Fixed call to app.param() function for optional params. Closes #682 2011-05-26 09:56:04 -07:00
Tj Holowaychuk
1ce43dd347 added failing test for #682 2011-05-26 09:48:07 -07:00
Tj Holowaychuk
d1bfe137d4 test to ensure catch of invalid uri 2011-05-25 15:50:32 -07:00
Tj Holowaychuk
9d7452cdc2 more tests 2011-05-25 10:56:56 -07:00
8 changed files with 129 additions and 24 deletions

View File

@@ -1,4 +1,11 @@
2.3.10 / 2011-05-27
==================
* Added `req.route`, exposing the current route
* Added _package.json_ generation support to `express(1)`
* Fixed call to `app.param()` function for optional params. Closes #682
2.3.9 / 2011-05-25
==================

View File

@@ -11,7 +11,7 @@ var fs = require('fs')
* Framework version.
*/
var version = '2.3.9';
var version = '2.3.10';
/**
* Add session support.
@@ -343,19 +343,22 @@ function createApplicationAt(path) {
// Template support
app = app.replace(':TEMPLATE', templateEngine);
write(path + '/app.js', app);
// package.json
var json = '{\n';
json += ' "name": "application-name"\n';
json += ' , "version": "0.0.1"\n';
json += ' , "private": true\n';
if (cssEngine || templateEngine) {
var comma = cssEngine ? ' ,' : '';
json += ' , "dependencies": {\n';
if (cssEngine) json += ' "' + cssEngine + '": ">= 0.0.1"\n';
if (templateEngine) json += ' ' + comma + ' "' + templateEngine + '": ">= 0.0.1"\n';
json += ' }\n';
}
json += '}';
// Suggestions
process.on('exit', function(){
if (cssEngine) {
console.log(' - make sure you have installed %s: \x1b[33m$ npm install %s\x1b[0m'
, cssEngine
, cssEngine);
}
console.log(' - make sure you have installed %s: \x1b[33m$ npm install %s\x1b[0m'
, templateEngine
, templateEngine);
});
write(path + '/package.json', json);
write(path + '/app.js', app);
});
}

View File

@@ -28,7 +28,7 @@ var exports = module.exports = connect.middleware;
* Framework version.
*/
exports.version = '2.3.9';
exports.version = '2.3.10';
/**
* Shortcut for `new Server(...)`.

View File

@@ -193,7 +193,7 @@ Router.prototype._dispatch = function(req, res, next){
}
// match route
route = self._match(req, i);
req.route = route = self._match(req, i);
// implied OPTIONS
if (!route && 'OPTIONS' == req.method) return self._options(req, res);
@@ -209,8 +209,8 @@ Router.prototype._dispatch = function(req, res, next){
(function param(err) {
var key = keys[i++]
, val = req.params[key]
, fn = params[key]
, val = key && req.params[key.name]
, fn = key && params[key.name]
, ret;
try {
@@ -218,7 +218,7 @@ Router.prototype._dispatch = function(req, res, next){
nextRoute();
} else if (err) {
next(err);
} else if (fn) {
} else if (fn && undefined !== val) {
fn(req, res, param, val);
} else if (key) {
param();
@@ -329,7 +329,7 @@ Router.prototype._match = function(req, i){
? decodeURIComponent(captures[j])
: captures[j];
if (key) {
route.params[key] = val;
route.params[key.name] = val;
} else {
route.params.push(val);
}

View File

@@ -70,7 +70,7 @@ function normalize(path, keys, sensitive) {
.concat('/?')
.replace(/\/\(/g, '(?:/')
.replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, function(_, slash, format, key, capture, optional){
keys.push(key);
keys.push({ name: key, optional: !! optional });
slash = slash || '';
return ''
+ (optional ? '' : slash)

View File

@@ -1,7 +1,7 @@
{
"name": "express",
"description": "Sinatra inspired web development framework",
"version": "2.3.9",
"version": "2.3.10",
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"contributors": [
{ "name": "TJ Holowaychuk", "email": "tj@vision-media.ca" },

View File

@@ -461,5 +461,17 @@ module.exports = {
assert.response(app,
{ url: '/another' },
{ body: 'got /another' });
},
'invalid chars': function(){
var app = express.createServer();
app.get('/:name', function(req, res, next){
res.send('invalid');
});
assert.response(app,
{ url: '/%a0' },
{ status: 500 });
}
};

View File

@@ -74,6 +74,38 @@ module.exports = {
});
},
'test precedence': function(){
var app = express.createServer();
var hits = [];
app.all('*', function(req, res, next){
hits.push('all');
next();
});
app.get('/foo', function(req, res, next){
hits.push('GET /foo');
next();
});
app.get('/foo', function(req, res, next){
hits.push('GET /foo2');
next();
});
app.put('/foo', function(req, res, next){
hits.push('PUT /foo');
next();
});
assert.response(app,
{ url: '/foo' },
function(){
hits.should.eql(['all', 'GET /foo', 'GET /foo2']);
});
},
'test named capture groups': function(){
var app = express.createServer();
@@ -106,7 +138,7 @@ module.exports = {
{ body: 'Cannot GET /user/ab' });
},
'test .param()': function(){
'test app.param()': function(){
var app = express.createServer();
var users = [
@@ -137,6 +169,35 @@ module.exports = {
{ url: '/user/1' },
{ body: 'user tobi' });
},
'test app.param() optional execution': function(beforeExit){
var app = express.createServer()
, calls = 0;
var months = ['Jan', 'Feb', 'Mar'];
app.param('month', function(req, res, next, n){
req.params.month = months[n];
++calls;
next();
});
app.get('/calendar/:month?', function(req, res, next){
res.send(req.params.month || months[0]);
});
assert.response(app,
{ url: '/calendar' },
{ body: 'Jan' });
assert.response(app,
{ url: '/calendar/1' },
{ body: 'Feb' });
beforeExit(function(){
calls.should.equal(1);
});
},
'test OPTIONS': function(){
var app = express.createServer();
@@ -168,7 +229,7 @@ module.exports = {
route.path.should.equal('/user/:id');
route.regexp.should.be.an.instanceof(RegExp);
route.method.should.equal('get');
route.keys.should.eql(['id']);
route.keys.should.eql([{ name: 'id', optional: false }]);
app.get('/user').should.have.length(1);
app.get('/user/:id').should.have.length(1);
@@ -230,7 +291,7 @@ module.exports = {
route.path.should.equal('/user/:id');
route.regexp.should.be.an.instanceof(RegExp);
route.method.should.equal('get');
route.keys.should.eql(['id']);
route.keys.should.eql([{ name: 'id', optional: false }]);
//route.params.id.should.equal('12');
app.match.get('/user').should.have.length(1);
@@ -298,5 +359,27 @@ module.exports = {
assert.response(app,
{ url: '/foo', method: 'OPTIONS' },
{ body: 'whatever', headers: { Allow: 'GET' }});
},
'test req.route': function(){
var app = express.createServer();
var routes = [];
app.get('/:foo?', function(req, res, next){
routes.push(req.route.path);
next();
});
app.get('/foo', function(req, res, next){
routes.push(req.route.path);
next();
});
assert.response(app,
{ url: '/foo' },
function(){
routes.should.eql(['/:foo?', '/foo']);
});
}
};