Middleware + Nesting + Formats working together #45

Closed
wants to merge 2 commits into
from

Projects

None yet

6 participants

@hunterloftis

Removed:

tests & implementation for mapped response formats, eg:

exports.show = { json: ..., xml: ... };

Added:

tests & implementation for middleware, eg:

exports.show = [
  middleware,
  function(req, res){
    res.send('show forum ' + req.params.forum);
  }
];

tests & implementation for middleware + formats + nesting, eg:

function middleware(req, res, next) {
  req.role = 'thread owner';
  next();
}

exports.show = [
  middleware,
  function(req, res){
    if (req.format === 'json') {
      res.send(JSON.stringify({
        thread: req.params.thread,
        forum: req.params.forum,
        user: req.user,           // Should not be populated because middleware should only run for the final route
        role: req.role
      }));
    }
    else {
      res.send('show thread ' + req.params.thread + ' of forum ' + req.params.forum + ' for ' + req.user + ', ' + req.role);
    }
  }
];

assert.response(app,
      { url: '/forums/1/threads/50.json' },
      { body: '{"thread":"50","forum":"1","role":"thread owner"}'
        , headers: { 'Content-Type': 'application/json' } });
@tj
Member
tj commented Jan 3, 2012

+1 from me, if anyone is uses the format callbacks let us know :D

we also need a nice way to apply middleware to all of them so it's not just as verbose as app.VERB

@elliotf
elliotf commented Jan 31, 2012

I would prefer to see #44 merged in, leaving the per-format callback, but I'd rather have middleware than format-style callbacks.

@timoxley
Contributor

+1

@luccastera

+1

@mciparelli

+1, and +1 to have some of a middleware option to pass a middleware to all the routes on the resource.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment