visionmedia/express-resource — pull request #45 by hunterloftis (2012-01-03 05:20:12+00:00)
Title: Middleware + Nesting + Formats working together

## Removed:

tests & implementation for mapped response formats, eg:

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

## Added:

tests & implementation for middleware, eg:

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

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

```javascript

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' } });
```