visionmedia / express-resource

Resourceful routing for Express
1.41k stars 140 forks source link

Middleware + Nesting + Formats working together #45

Closed hunterloftis closed 10 years ago

hunterloftis commented 12 years ago

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 commented 12 years ago

+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 commented 12 years ago

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

timoxley commented 12 years ago

+1

luccastera commented 12 years ago

+1

mciparelli commented 12 years ago

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