index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. * body-parser
  3. * Copyright(c) 2014-2015 Douglas Christopher Wilson
  4. * MIT Licensed
  5. */
  6. 'use strict'
  7. /**
  8. * Module dependencies.
  9. * @private
  10. */
  11. var deprecate = require('depd')('body-parser')
  12. /**
  13. * Cache of loaded parsers.
  14. * @private
  15. */
  16. var parsers = Object.create(null)
  17. /**
  18. * @typedef Parsers
  19. * @type {function}
  20. * @property {function} json
  21. * @property {function} raw
  22. * @property {function} text
  23. * @property {function} urlencoded
  24. */
  25. /**
  26. * Module exports.
  27. * @type {Parsers}
  28. */
  29. exports = module.exports = deprecate.function(bodyParser,
  30. 'bodyParser: use individual json/urlencoded middlewares')
  31. /**
  32. * JSON parser.
  33. * @public
  34. */
  35. Object.defineProperty(exports, 'json', {
  36. configurable: true,
  37. enumerable: true,
  38. get: createParserGetter('json')
  39. })
  40. /**
  41. * Raw parser.
  42. * @public
  43. */
  44. Object.defineProperty(exports, 'raw', {
  45. configurable: true,
  46. enumerable: true,
  47. get: createParserGetter('raw')
  48. })
  49. /**
  50. * Text parser.
  51. * @public
  52. */
  53. Object.defineProperty(exports, 'text', {
  54. configurable: true,
  55. enumerable: true,
  56. get: createParserGetter('text')
  57. })
  58. /**
  59. * URL-encoded parser.
  60. * @public
  61. */
  62. Object.defineProperty(exports, 'urlencoded', {
  63. configurable: true,
  64. enumerable: true,
  65. get: createParserGetter('urlencoded')
  66. })
  67. /**
  68. * Create a middleware to parse json and urlencoded bodies.
  69. *
  70. * @param {object} [options]
  71. * @return {function}
  72. * @deprecated
  73. * @public
  74. */
  75. function bodyParser (options) {
  76. var opts = {}
  77. // exclude type option
  78. if (options) {
  79. for (var prop in options) {
  80. if (prop !== 'type') {
  81. opts[prop] = options[prop]
  82. }
  83. }
  84. }
  85. var _urlencoded = exports.urlencoded(opts)
  86. var _json = exports.json(opts)
  87. return function bodyParser (req, res, next) {
  88. _json(req, res, function (err) {
  89. if (err) return next(err)
  90. _urlencoded(req, res, next)
  91. })
  92. }
  93. }
  94. /**
  95. * Create a getter for loading a parser.
  96. * @private
  97. */
  98. function createParserGetter (name) {
  99. return function get () {
  100. return loadParser(name)
  101. }
  102. }
  103. /**
  104. * Load a parser module.
  105. * @private
  106. */
  107. function loadParser (parserName) {
  108. var parser = parsers[parserName]
  109. if (parser !== undefined) {
  110. return parser
  111. }
  112. // this uses a switch for static require analysis
  113. switch (parserName) {
  114. case 'json':
  115. parser = require('./lib/types/json')
  116. break
  117. case 'raw':
  118. parser = require('./lib/types/raw')
  119. break
  120. case 'text':
  121. parser = require('./lib/types/text')
  122. break
  123. case 'urlencoded':
  124. parser = require('./lib/types/urlencoded')
  125. break
  126. }
  127. // store to prevent invoking require()
  128. return (parsers[parserName] = parser)
  129. }