Skip to content

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

Open
@razvanz

Description

@razvanz

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

I saw in the last comment 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:

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

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

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

// 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?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions