123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 'use strict'
- var deprecate = require('depd')('body-parser')
- var parsers = Object.create(null)
- exports = module.exports = deprecate.function(bodyParser,
- 'bodyParser: use individual json/urlencoded middlewares')
- Object.defineProperty(exports, 'json', {
- configurable: true,
- enumerable: true,
- get: createParserGetter('json')
- })
- Object.defineProperty(exports, 'raw', {
- configurable: true,
- enumerable: true,
- get: createParserGetter('raw')
- })
- Object.defineProperty(exports, 'text', {
- configurable: true,
- enumerable: true,
- get: createParserGetter('text')
- })
- Object.defineProperty(exports, 'urlencoded', {
- configurable: true,
- enumerable: true,
- get: createParserGetter('urlencoded')
- })
- function bodyParser (options) {
-
- var opts = Object.create(options || null, {
- type: {
- configurable: true,
- enumerable: true,
- value: undefined,
- writable: true
- }
- })
- var _urlencoded = exports.urlencoded(opts)
- var _json = exports.json(opts)
- return function bodyParser (req, res, next) {
- _json(req, res, function (err) {
- if (err) return next(err)
- _urlencoded(req, res, next)
- })
- }
- }
- function createParserGetter (name) {
- return function get () {
- return loadParser(name)
- }
- }
- function loadParser (parserName) {
- var parser = parsers[parserName]
- if (parser !== undefined) {
- return parser
- }
-
- switch (parserName) {
- case 'json':
- parser = require('./lib/types/json')
- break
- case 'raw':
- parser = require('./lib/types/raw')
- break
- case 'text':
- parser = require('./lib/types/text')
- break
- case 'urlencoded':
- parser = require('./lib/types/urlencoded')
- break
- }
-
- return (parsers[parserName] = parser)
- }
|