Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created July 17, 2014 01:11
Show Gist options
  • Save Raynos/f483d2d939ea13f23295 to your computer and use it in GitHub Desktop.
Save Raynos/f483d2d939ea13f23295 to your computer and use it in GitHub Desktop.
var jsonBody = require('body/json');
/* usage
```js
router.addRoute('/foo/bar',
bodyParser(function (req, res, opts, cb)))
```
using multiple "middlewares"
```js
router.addRoute('/foo/bar',
bodyParser(
getSession(
myRouteHandler
)
)
)
```
using a sugar module
```js
var compose = require('composite');
router.addRoute('/foo/bar', compose(
getSession,
bodyParser
)(myRouteHandler))
```
*/
module.exports = bodyParser;
function bodyParser(handler) {
return function (req, res, opts, cb) {
jsonBody(req, res, function (err, body) {
if (err) {
return cb(err);
}
opts.body = body;
handler(req, res, opts, cb);
});
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment