You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.4 KiB

  1. /*!
  2. * Connect - bodyParser
  3. * Copyright(c) 2010 Sencha Inc.
  4. * Copyright(c) 2011 TJ Holowaychuk
  5. * MIT Licensed
  6. */
  7. /**
  8. * Module dependencies.
  9. */
  10. var multipart = require('./multipart')
  11. , urlencoded = require('./urlencoded')
  12. , json = require('./json');
  13. /**
  14. * Body parser:
  15. *
  16. * Parse request bodies, supports _application/json_,
  17. * _application/x-www-form-urlencoded_, and _multipart/form-data_.
  18. *
  19. * This is equivalent to:
  20. *
  21. * app.use(connect.json());
  22. * app.use(connect.urlencoded());
  23. * app.use(connect.multipart());
  24. *
  25. * Examples:
  26. *
  27. * connect()
  28. * .use(connect.bodyParser())
  29. * .use(function(req, res) {
  30. * res.end('viewing user ' + req.body.user.name);
  31. * });
  32. *
  33. * $ curl -d 'user[name]=tj' http://local/
  34. * $ curl -d '{"user":{"name":"tj"}}' -H "Content-Type: application/json" http://local/
  35. *
  36. * View [json](json.html), [urlencoded](urlencoded.html), and [multipart](multipart.html) for more info.
  37. *
  38. * @param {Object} options
  39. * @return {Function}
  40. * @api public
  41. */
  42. exports = module.exports = function bodyParser(options){
  43. var _urlencoded = urlencoded(options)
  44. , _multipart = multipart(options)
  45. , _json = json(options);
  46. return function bodyParser(req, res, next) {
  47. _json(req, res, function(err){
  48. if (err) return next(err);
  49. _urlencoded(req, res, function(err){
  50. if (err) return next(err);
  51. _multipart(req, res, next);
  52. });
  53. });
  54. }
  55. };