source: trip-planner-front/node_modules/body-parser/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

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