expressjs / expressjs/express

Multiple callbacks on app.param. Eg: app.param('resource', cb1, cb2)

Open
#2,603 2 comments 0 reactions 0 assignees View on GitHub
discuss ideas
Dominant language
JavaScript
Stars
69.5k
Forks
25k
Avg merge
4d 20h
Merged PRs (30d)
9

Description

Duplicate of #2181, but since it has been locked I have to create a new issue.

I saw in the [last comment](https://github.com/strongloop/express/issues/2181#issuecomment-46851349) from #2181 that a use case was required:

Considering one would want to do both authorization and loading for en entity, having both callbacks defined in the `app.param()` would be more readable, as well as provide controller reuse:

``` javascript
app.all('/app/entity*', permissions.initEntityAuth);

app.all('/app/entity/:entityid*',
entity.authorize,
entity.loadEntity,
function (req, res, next) {
return next('route');
});

app.route('/app/entity/:entityid')
.get(...)
.put(...);
```

vs

``` javascript
app.all('/app/entity*', permissions.initEntityAuth);

app.param('entityid',
// everything done inside this controller. Reuse level 0.
entity.authorizeAndLoad);

app.route('/app/entity/:entityid')
.get(...)
.put(...);
```

vs

``` javascript
app.all('/app/entity*', permissions.initEntityAuth);

app.param('entityid',
// Ohh, no. I have to maintain an async_wrap now
some_async_wrap(entity.authorize, entity.loadEntity));

app.route('/app/entity/:entityid')
.get(...)
.put(...);
```

vs

``` javascript
// It seems to be much more readable as well as keeping the code reuse
app.all('/app/entity*', permissions.initEntityAuth);

app.param('entityid',
entity.authorize,
entity.loadEntity);

app.route('/app/entity/:entityid')
.get(...)
.put(...);
```

What do you think?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.