root / HServer / 00.Server / 00.Program / node_modules / body-parser / README.md
이력 | 보기 | 이력해설 | 다운로드 (16.5 KB)
| 1 | 39 | HKM | # body-parser |
|---|---|---|---|
| 2 | |||
| 3 | [![NPM Version][npm-image]][npm-url] |
||
| 4 | [![NPM Downloads][downloads-image]][downloads-url] |
||
| 5 | [![Build Status][travis-image]][travis-url] |
||
| 6 | [![Test Coverage][coveralls-image]][coveralls-url] |
||
| 7 | [![Gratipay][gratipay-image]][gratipay-url] |
||
| 8 | |||
| 9 | Node.js body parsing middleware. |
||
| 10 | |||
| 11 | Parse incoming request bodies in a middleware before your handlers, available |
||
| 12 | under the `req.body` property. |
||
| 13 | |||
| 14 | [Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/). |
||
| 15 | |||
| 16 | _This does not handle multipart bodies_, due to their complex and typically |
||
| 17 | large nature. For multipart bodies, you may be interested in the following |
||
| 18 | modules: |
||
| 19 | |||
| 20 | * [busboy](https://www.npmjs.org/package/busboy#readme) and |
||
| 21 | [connect-busboy](https://www.npmjs.org/package/connect-busboy#readme) |
||
| 22 | * [multiparty](https://www.npmjs.org/package/multiparty#readme) and |
||
| 23 | [connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme) |
||
| 24 | * [formidable](https://www.npmjs.org/package/formidable#readme) |
||
| 25 | * [multer](https://www.npmjs.org/package/multer#readme) |
||
| 26 | |||
| 27 | This module provides the following parsers: |
||
| 28 | |||
| 29 | * [JSON body parser](#bodyparserjsonoptions) |
||
| 30 | * [Raw body parser](#bodyparserrawoptions) |
||
| 31 | * [Text body parser](#bodyparsertextoptions) |
||
| 32 | * [URL-encoded form body parser](#bodyparserurlencodedoptions) |
||
| 33 | |||
| 34 | Other body parsers you might be interested in: |
||
| 35 | |||
| 36 | - [body](https://www.npmjs.org/package/body#readme) |
||
| 37 | - [co-body](https://www.npmjs.org/package/co-body#readme) |
||
| 38 | |||
| 39 | ## Installation |
||
| 40 | |||
| 41 | ```sh |
||
| 42 | $ npm install body-parser |
||
| 43 | ``` |
||
| 44 | |||
| 45 | ## API |
||
| 46 | |||
| 47 | <!-- eslint-disable no-unused-vars --> |
||
| 48 | |||
| 49 | ```js |
||
| 50 | var bodyParser = require('body-parser')
|
||
| 51 | ``` |
||
| 52 | |||
| 53 | The `bodyParser` object exposes various factories to create middlewares. All |
||
| 54 | middlewares will populate the `req.body` property with the parsed body when |
||
| 55 | the `Content-Type` request header matches the `type` option, or an empty |
||
| 56 | object (`{}`) if there was no body to parse, the `Content-Type` was not matched,
|
||
| 57 | or an error occurred. |
||
| 58 | |||
| 59 | The various errors returned by this module are described in the |
||
| 60 | [errors section](#errors). |
||
| 61 | |||
| 62 | ### bodyParser.json([options]) |
||
| 63 | |||
| 64 | Returns middleware that only parses `json` and only looks at requests where |
||
| 65 | the `Content-Type` header matches the `type` option. This parser accepts any |
||
| 66 | Unicode encoding of the body and supports automatic inflation of `gzip` and |
||
| 67 | `deflate` encodings. |
||
| 68 | |||
| 69 | A new `body` object containing the parsed data is populated on the `request` |
||
| 70 | object after the middleware (i.e. `req.body`). |
||
| 71 | |||
| 72 | #### Options |
||
| 73 | |||
| 74 | The `json` function takes an optional `options` object that may contain any of |
||
| 75 | the following keys: |
||
| 76 | |||
| 77 | ##### inflate |
||
| 78 | |||
| 79 | When set to `true`, then deflated (compressed) bodies will be inflated; when |
||
| 80 | `false`, deflated bodies are rejected. Defaults to `true`. |
||
| 81 | |||
| 82 | ##### limit |
||
| 83 | |||
| 84 | Controls the maximum request body size. If this is a number, then the value |
||
| 85 | specifies the number of bytes; if it is a string, the value is passed to the |
||
| 86 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults |
||
| 87 | to `'100kb'`. |
||
| 88 | |||
| 89 | ##### reviver |
||
| 90 | |||
| 91 | The `reviver` option is passed directly to `JSON.parse` as the second |
||
| 92 | argument. You can find more information on this argument |
||
| 93 | [in the MDN documentation about JSON.parse](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter). |
||
| 94 | |||
| 95 | ##### strict |
||
| 96 | |||
| 97 | When set to `true`, will only accept arrays and objects; when `false` will |
||
| 98 | accept anything `JSON.parse` accepts. Defaults to `true`. |
||
| 99 | |||
| 100 | ##### type |
||
| 101 | |||
| 102 | The `type` option is used to determine what media type the middleware will |
||
| 103 | parse. This option can be a function or a string. If a string, `type` option |
||
| 104 | is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) |
||
| 105 | library and this can be an extension name (like `json`), a mime type (like |
||
| 106 | `application/json`), or a mime type with a wildcard (like `*/*` or `*/json`). |
||
| 107 | If a function, the `type` option is called as `fn(req)` and the request is |
||
| 108 | parsed if it returns a truthy value. Defaults to `application/json`. |
||
| 109 | |||
| 110 | ##### verify |
||
| 111 | |||
| 112 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, |
||
| 113 | where `buf` is a `Buffer` of the raw request body and `encoding` is the |
||
| 114 | encoding of the request. The parsing can be aborted by throwing an error. |
||
| 115 | |||
| 116 | ### bodyParser.raw([options]) |
||
| 117 | |||
| 118 | Returns middleware that parses all bodies as a `Buffer` and only looks at |
||
| 119 | requests where the `Content-Type` header matches the `type` option. This |
||
| 120 | parser supports automatic inflation of `gzip` and `deflate` encodings. |
||
| 121 | |||
| 122 | A new `body` object containing the parsed data is populated on the `request` |
||
| 123 | object after the middleware (i.e. `req.body`). This will be a `Buffer` object |
||
| 124 | of the body. |
||
| 125 | |||
| 126 | #### Options |
||
| 127 | |||
| 128 | The `raw` function takes an optional `options` object that may contain any of |
||
| 129 | the following keys: |
||
| 130 | |||
| 131 | ##### inflate |
||
| 132 | |||
| 133 | When set to `true`, then deflated (compressed) bodies will be inflated; when |
||
| 134 | `false`, deflated bodies are rejected. Defaults to `true`. |
||
| 135 | |||
| 136 | ##### limit |
||
| 137 | |||
| 138 | Controls the maximum request body size. If this is a number, then the value |
||
| 139 | specifies the number of bytes; if it is a string, the value is passed to the |
||
| 140 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults |
||
| 141 | to `'100kb'`. |
||
| 142 | |||
| 143 | ##### type |
||
| 144 | |||
| 145 | The `type` option is used to determine what media type the middleware will |
||
| 146 | parse. This option can be a function or a string. If a string, `type` option |
||
| 147 | is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) |
||
| 148 | library and this can be an extension name (like `bin`), a mime type (like |
||
| 149 | `application/octet-stream`), or a mime type with a wildcard (like `*/*` or |
||
| 150 | `application/*`). If a function, the `type` option is called as `fn(req)` |
||
| 151 | and the request is parsed if it returns a truthy value. Defaults to |
||
| 152 | `application/octet-stream`. |
||
| 153 | |||
| 154 | ##### verify |
||
| 155 | |||
| 156 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, |
||
| 157 | where `buf` is a `Buffer` of the raw request body and `encoding` is the |
||
| 158 | encoding of the request. The parsing can be aborted by throwing an error. |
||
| 159 | |||
| 160 | ### bodyParser.text([options]) |
||
| 161 | |||
| 162 | Returns middleware that parses all bodies as a string and only looks at |
||
| 163 | requests where the `Content-Type` header matches the `type` option. This |
||
| 164 | parser supports automatic inflation of `gzip` and `deflate` encodings. |
||
| 165 | |||
| 166 | A new `body` string containing the parsed data is populated on the `request` |
||
| 167 | object after the middleware (i.e. `req.body`). This will be a string of the |
||
| 168 | body. |
||
| 169 | |||
| 170 | #### Options |
||
| 171 | |||
| 172 | The `text` function takes an optional `options` object that may contain any of |
||
| 173 | the following keys: |
||
| 174 | |||
| 175 | ##### defaultCharset |
||
| 176 | |||
| 177 | Specify the default character set for the text content if the charset is not |
||
| 178 | specified in the `Content-Type` header of the request. Defaults to `utf-8`. |
||
| 179 | |||
| 180 | ##### inflate |
||
| 181 | |||
| 182 | When set to `true`, then deflated (compressed) bodies will be inflated; when |
||
| 183 | `false`, deflated bodies are rejected. Defaults to `true`. |
||
| 184 | |||
| 185 | ##### limit |
||
| 186 | |||
| 187 | Controls the maximum request body size. If this is a number, then the value |
||
| 188 | specifies the number of bytes; if it is a string, the value is passed to the |
||
| 189 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults |
||
| 190 | to `'100kb'`. |
||
| 191 | |||
| 192 | ##### type |
||
| 193 | |||
| 194 | The `type` option is used to determine what media type the middleware will |
||
| 195 | parse. This option can be a function or a string. If a string, `type` option |
||
| 196 | is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) |
||
| 197 | library and this can be an extension name (like `txt`), a mime type (like |
||
| 198 | `text/plain`), or a mime type with a wildcard (like `*/*` or `text/*`). |
||
| 199 | If a function, the `type` option is called as `fn(req)` and the request is |
||
| 200 | parsed if it returns a truthy value. Defaults to `text/plain`. |
||
| 201 | |||
| 202 | ##### verify |
||
| 203 | |||
| 204 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, |
||
| 205 | where `buf` is a `Buffer` of the raw request body and `encoding` is the |
||
| 206 | encoding of the request. The parsing can be aborted by throwing an error. |
||
| 207 | |||
| 208 | ### bodyParser.urlencoded([options]) |
||
| 209 | |||
| 210 | Returns middleware that only parses `urlencoded` bodies and only looks at |
||
| 211 | requests where the `Content-Type` header matches the `type` option. This |
||
| 212 | parser accepts only UTF-8 encoding of the body and supports automatic |
||
| 213 | inflation of `gzip` and `deflate` encodings. |
||
| 214 | |||
| 215 | A new `body` object containing the parsed data is populated on the `request` |
||
| 216 | object after the middleware (i.e. `req.body`). This object will contain |
||
| 217 | key-value pairs, where the value can be a string or array (when `extended` is |
||
| 218 | `false`), or any type (when `extended` is `true`). |
||
| 219 | |||
| 220 | #### Options |
||
| 221 | |||
| 222 | The `urlencoded` function takes an optional `options` object that may contain |
||
| 223 | any of the following keys: |
||
| 224 | |||
| 225 | ##### extended |
||
| 226 | |||
| 227 | The `extended` option allows to choose between parsing the URL-encoded data |
||
| 228 | with the `querystring` library (when `false`) or the `qs` library (when |
||
| 229 | `true`). The "extended" syntax allows for rich objects and arrays to be |
||
| 230 | encoded into the URL-encoded format, allowing for a JSON-like experience |
||
| 231 | with URL-encoded. For more information, please |
||
| 232 | [see the qs library](https://www.npmjs.org/package/qs#readme). |
||
| 233 | |||
| 234 | Defaults to `true`, but using the default has been deprecated. Please |
||
| 235 | research into the difference between `qs` and `querystring` and choose the |
||
| 236 | appropriate setting. |
||
| 237 | |||
| 238 | ##### inflate |
||
| 239 | |||
| 240 | When set to `true`, then deflated (compressed) bodies will be inflated; when |
||
| 241 | `false`, deflated bodies are rejected. Defaults to `true`. |
||
| 242 | |||
| 243 | ##### limit |
||
| 244 | |||
| 245 | Controls the maximum request body size. If this is a number, then the value |
||
| 246 | specifies the number of bytes; if it is a string, the value is passed to the |
||
| 247 | [bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults |
||
| 248 | to `'100kb'`. |
||
| 249 | |||
| 250 | ##### parameterLimit |
||
| 251 | |||
| 252 | The `parameterLimit` option controls the maximum number of parameters that |
||
| 253 | are allowed in the URL-encoded data. If a request contains more parameters |
||
| 254 | than this value, a 413 will be returned to the client. Defaults to `1000`. |
||
| 255 | |||
| 256 | ##### type |
||
| 257 | |||
| 258 | The `type` option is used to determine what media type the middleware will |
||
| 259 | parse. This option can be a function or a string. If a string, `type` option |
||
| 260 | is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) |
||
| 261 | library and this can be an extension name (like `urlencoded`), a mime type (like |
||
| 262 | `application/x-www-form-urlencoded`), or a mime type with a wildcard (like |
||
| 263 | `*/x-www-form-urlencoded`). If a function, the `type` option is called as |
||
| 264 | `fn(req)` and the request is parsed if it returns a truthy value. Defaults |
||
| 265 | to `application/x-www-form-urlencoded`. |
||
| 266 | |||
| 267 | ##### verify |
||
| 268 | |||
| 269 | The `verify` option, if supplied, is called as `verify(req, res, buf, encoding)`, |
||
| 270 | where `buf` is a `Buffer` of the raw request body and `encoding` is the |
||
| 271 | encoding of the request. The parsing can be aborted by throwing an error. |
||
| 272 | |||
| 273 | ## Errors |
||
| 274 | |||
| 275 | The middlewares provided by this module create errors depending on the error |
||
| 276 | condition during parsing. The errors will typically have a `status`/`statusCode` |
||
| 277 | property that contains the suggested HTTP response code, an `expose` property |
||
| 278 | to determine if the `message` property should be displayed to the client, a |
||
| 279 | `type` property to determine the type of error without matching against the |
||
| 280 | `message`, and a `body` property containing the read body, if available. |
||
| 281 | |||
| 282 | The following are the common errors emitted, though any error can come through |
||
| 283 | for various reasons. |
||
| 284 | |||
| 285 | ### content encoding unsupported |
||
| 286 | |||
| 287 | This error will occur when the request had a `Content-Encoding` header that |
||
| 288 | contained an encoding but the "inflation" option was set to `false`. The |
||
| 289 | `status` property is set to `415`, the `type` property is set to |
||
| 290 | `'encoding.unsupported'`, and the `charset` property will be set to the |
||
| 291 | encoding that is unsupported. |
||
| 292 | |||
| 293 | ### request aborted |
||
| 294 | |||
| 295 | This error will occur when the request is aborted by the client before reading |
||
| 296 | the body has finished. The `received` property will be set to the number of |
||
| 297 | bytes received before the request was aborted and the `expected` property is |
||
| 298 | set to the number of expected bytes. The `status` property is set to `400` |
||
| 299 | and `type` property is set to `'request.aborted'`. |
||
| 300 | |||
| 301 | ### request entity too large |
||
| 302 | |||
| 303 | This error will occur when the request body's size is larger than the "limit" |
||
| 304 | option. The `limit` property will be set to the byte limit and the `length` |
||
| 305 | property will be set to the request body's length. The `status` property is |
||
| 306 | set to `413` and the `type` property is set to `'entity.too.large'`. |
||
| 307 | |||
| 308 | ### request size did not match content length |
||
| 309 | |||
| 310 | This error will occur when the request's length did not match the length from |
||
| 311 | the `Content-Length` header. This typically occurs when the request is malformed, |
||
| 312 | typically when the `Content-Length` header was calculated based on characters |
||
| 313 | instead of bytes. The `status` property is set to `400` and the `type` property |
||
| 314 | is set to `'request.size.invalid'`. |
||
| 315 | |||
| 316 | ### stream encoding should not be set |
||
| 317 | |||
| 318 | This error will occur when something called the `req.setEncoding` method prior |
||
| 319 | to this middleware. This module operates directly on bytes only and you cannot |
||
| 320 | call `req.setEncoding` when using this module. The `status` property is set to |
||
| 321 | `500` and the `type` property is set to `'stream.encoding.set'`. |
||
| 322 | |||
| 323 | ### too many parameters |
||
| 324 | |||
| 325 | This error will occur when the content of the request exceeds the configured |
||
| 326 | `parameterLimit` for the `urlencoded` parser. The `status` property is set to |
||
| 327 | `413` and the `type` property is set to `'parameters.too.many'`. |
||
| 328 | |||
| 329 | ### unsupported charset "BOGUS" |
||
| 330 | |||
| 331 | This error will occur when the request had a charset parameter in the |
||
| 332 | `Content-Type` header, but the `iconv-lite` module does not support it OR the |
||
| 333 | parser does not support it. The charset is contained in the message as well |
||
| 334 | as in the `charset` property. The `status` property is set to `415`, the |
||
| 335 | `type` property is set to `'charset.unsupported'`, and the `charset` property |
||
| 336 | is set to the charset that is unsupported. |
||
| 337 | |||
| 338 | ### unsupported content encoding "bogus" |
||
| 339 | |||
| 340 | This error will occur when the request had a `Content-Encoding` header that |
||
| 341 | contained an unsupported encoding. The encoding is contained in the message |
||
| 342 | as well as in the `encoding` property. The `status` property is set to `415`, |
||
| 343 | the `type` property is set to `'encoding.unsupported'`, and the `encoding` |
||
| 344 | property is set to the encoding that is unsupported. |
||
| 345 | |||
| 346 | ## Examples |
||
| 347 | |||
| 348 | ### Express/Connect top-level generic |
||
| 349 | |||
| 350 | This example demonstrates adding a generic JSON and URL-encoded parser as a |
||
| 351 | top-level middleware, which will parse the bodies of all incoming requests. |
||
| 352 | This is the simplest setup. |
||
| 353 | |||
| 354 | ```js |
||
| 355 | var express = require('express')
|
||
| 356 | var bodyParser = require('body-parser')
|
||
| 357 | |||
| 358 | var app = express() |
||
| 359 | |||
| 360 | // parse application/x-www-form-urlencoded |
||
| 361 | app.use(bodyParser.urlencoded({ extended: false }))
|
||
| 362 | |||
| 363 | // parse application/json |
||
| 364 | app.use(bodyParser.json()) |
||
| 365 | |||
| 366 | app.use(function (req, res) {
|
||
| 367 | res.setHeader('Content-Type', 'text/plain')
|
||
| 368 | res.write('you posted:\n')
|
||
| 369 | res.end(JSON.stringify(req.body, null, 2)) |
||
| 370 | }) |
||
| 371 | ``` |
||
| 372 | |||
| 373 | ### Express route-specific |
||
| 374 | |||
| 375 | This example demonstrates adding body parsers specifically to the routes that |
||
| 376 | need them. In general, this is the most recommended way to use body-parser with |
||
| 377 | Express. |
||
| 378 | |||
| 379 | ```js |
||
| 380 | var express = require('express')
|
||
| 381 | var bodyParser = require('body-parser')
|
||
| 382 | |||
| 383 | var app = express() |
||
| 384 | |||
| 385 | // create application/json parser |
||
| 386 | var jsonParser = bodyParser.json() |
||
| 387 | |||
| 388 | // create application/x-www-form-urlencoded parser |
||
| 389 | var urlencodedParser = bodyParser.urlencoded({ extended: false })
|
||
| 390 | |||
| 391 | // POST /login gets urlencoded bodies |
||
| 392 | app.post('/login', urlencodedParser, function (req, res) {
|
||
| 393 | if (!req.body) return res.sendStatus(400) |
||
| 394 | res.send('welcome, ' + req.body.username)
|
||
| 395 | }) |
||
| 396 | |||
| 397 | // POST /api/users gets JSON bodies |
||
| 398 | app.post('/api/users', jsonParser, function (req, res) {
|
||
| 399 | if (!req.body) return res.sendStatus(400) |
||
| 400 | // create user in req.body |
||
| 401 | }) |
||
| 402 | ``` |
||
| 403 | |||
| 404 | ### Change accepted type for parsers |
||
| 405 | |||
| 406 | All the parsers accept a `type` option which allows you to change the |
||
| 407 | `Content-Type` that the middleware will parse. |
||
| 408 | |||
| 409 | ```js |
||
| 410 | var express = require('express')
|
||
| 411 | var bodyParser = require('body-parser')
|
||
| 412 | |||
| 413 | var app = express() |
||
| 414 | |||
| 415 | // parse various different custom JSON types as JSON |
||
| 416 | app.use(bodyParser.json({ type: 'application/*+json' }))
|
||
| 417 | |||
| 418 | // parse some custom thing into a Buffer |
||
| 419 | app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
|
||
| 420 | |||
| 421 | // parse an HTML body into a string |
||
| 422 | app.use(bodyParser.text({ type: 'text/html' }))
|
||
| 423 | ``` |
||
| 424 | |||
| 425 | ## License |
||
| 426 | |||
| 427 | [MIT](LICENSE) |
||
| 428 | |||
| 429 | [npm-image]: https://img.shields.io/npm/v/body-parser.svg |
||
| 430 | [npm-url]: https://npmjs.org/package/body-parser |
||
| 431 | [travis-image]: https://img.shields.io/travis/expressjs/body-parser/master.svg |
||
| 432 | [travis-url]: https://travis-ci.org/expressjs/body-parser |
||
| 433 | [coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser/master.svg |
||
| 434 | [coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master |
||
| 435 | [downloads-image]: https://img.shields.io/npm/dm/body-parser.svg |
||
| 436 | [downloads-url]: https://npmjs.org/package/body-parser |
||
| 437 | [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg |
||
| 438 | [gratipay-url]: https://www.gratipay.com/dougwilson/ |