Skip to content
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
1 change: 1 addition & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ $ npm install express
* [Github Organization](https://github.com/expressjs) for Official Middleware & Modules
* Visit the [Wiki](https://github.com/strongloop/express/wiki)
* [Google Group](https://groups.google.com/group/express-js) for discussion
* [Gitter](https://gitter.im/strongloop/express) for support and discussion
* [Русскоязычная документация](http://jsman.ru/express/)

**PROTIP** Be sure to read [Migrating from 3.x to 4.x](https://github.com/strongloop/express/wiki/Migrating-from-3.x-to-4.x) as well as [New features in 4.x](https://github.com/strongloop/express/wiki/New-features-in-4.x).
Expand Down
23 changes: 17 additions & 6 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -312,23 +312,34 @@ app.engine = function engine(ext, fn) {
* See the Router#param() docs for more details.
*
* @param {String|Array} name
* @param {Function} fn
* @param {Function} ...
* @return {app} for chaining
* @public
*/

app.param = function param(name, fn) {
app.param = function param(name, fn){
var args = slice.apply(arguments);
var name = args[0];
var fns = args.slice(1);

this.lazyrouter();

if (Array.isArray(name)) {
for (var i = 0; i < name.length; i++) {
this.param(name[i], fn);
}
name.forEach(function(key) {
var params = [key].concat(fns);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[key].concat(fns) will inadvertinately allow for the arguments to be arrays, because it'll flatten in certain cases, i.e.:

$ node -pe '[2].concat([1,2])'
[ 2, 1, 2 ]

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that is how apply works. I mean we need an array for apply to work

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm talking about concat; you need to use something else that won't flatten the fns array.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, nvm, I didn't look all the way, sorry!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. (Not related) Is my new test case alright?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Not related) Is my new test case alright?

Yep

this.param.apply(this, params);
}, this);

return this;
}

this._router.param(name, fn);
if (fns.length == 0) {
this._router.param(name);
} else {
fns.forEach(function(fn){
this._router.param(name, fn);
}, this);
}

return this;
};
Expand Down
24 changes: 24 additions & 0 deletions test/app.param.js
Original file line number Diff line number Diff line change
Expand Up @@ -364,4 +364,28 @@ describe('app', function(){
.expect('1 2 bob', done);
})
})

describe('.param(name, fns)', function(){
it('should map logic for a series of functions', function(done){
var app = express();
var fns = [];

app.param('id', function(req, res, next, id){
fns.push(1);
next();
}, function(req, res, next, id){
fns.push(2);
next();
});

app.get('/user/:id', function(req, res){
res.send('' + fns.join(''));
});

request(app)
.get('/user/123')
.expect('12', done);
})
})

})