index.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*!
  2. * body-parser
  3. * Copyright(c) 2014 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. /**
  7. * Module dependencies.
  8. */
  9. var deprecate = require('depd')('body-parser')
  10. var fs = require('fs')
  11. var path = require('path')
  12. /**
  13. * Module exports.
  14. */
  15. exports = module.exports = deprecate.function(bodyParser,
  16. 'bodyParser: use individual json/urlencoded middlewares')
  17. /**
  18. * Path to the parser modules.
  19. */
  20. var parsersDir = path.join(__dirname, 'lib', 'types')
  21. /**
  22. * Auto-load bundled parsers with getters.
  23. */
  24. fs.readdirSync(parsersDir).forEach(function onfilename(filename) {
  25. if (!/\.js$/.test(filename)) return
  26. var loc = path.resolve(parsersDir, filename)
  27. var mod
  28. var name = path.basename(filename, '.js')
  29. function load() {
  30. if (mod) {
  31. return mod
  32. }
  33. return mod = require(loc)
  34. }
  35. Object.defineProperty(exports, name, {
  36. configurable: true,
  37. enumerable: true,
  38. get: load
  39. })
  40. })
  41. /**
  42. * Create a middleware to parse json and urlencoded bodies.
  43. *
  44. * @param {object} [options]
  45. * @return {function}
  46. * @deprecated
  47. * @api public
  48. */
  49. function bodyParser(options){
  50. var opts = {}
  51. options = options || {}
  52. // exclude type option
  53. for (var prop in options) {
  54. if ('type' !== prop) {
  55. opts[prop] = options[prop]
  56. }
  57. }
  58. var _urlencoded = exports.urlencoded(opts)
  59. var _json = exports.json(opts)
  60. return function bodyParser(req, res, next) {
  61. _json(req, res, function(err){
  62. if (err) return next(err);
  63. _urlencoded(req, res, next);
  64. });
  65. }
  66. }