PageRenderTime 11ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/body-parser/README.md

https://gitlab.com/Erdrix/archiveServices
Markdown | 192 lines | 129 code | 63 blank | 0 comment | 0 complexity | 5f52aa009cece74929186ade7e14a05b MD5 | raw file
  1. # body-parser
  2. [![NPM Version][npm-image]][npm-url]
  3. [![NPM Downloads][downloads-image]][downloads-url]
  4. [![Build Status][travis-image]][travis-url]
  5. [![Test Coverage][coveralls-image]][coveralls-url]
  6. [![Gratipay][gratipay-image]][gratipay-url]
  7. Node.js body parsing middleware.
  8. This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:
  9. - [busboy](https://www.npmjs.org/package/busboy#readme) and [connect-busboy](https://www.npmjs.org/package/connect-busboy#readme)
  10. - [multiparty](https://www.npmjs.org/package/multiparty#readme) and [connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme)
  11. - [formidable](https://www.npmjs.org/package/formidable#readme)
  12. - [multer](https://www.npmjs.org/package/multer#readme)
  13. Other body parsers you might be interested in:
  14. - [body](https://www.npmjs.org/package/body#readme)
  15. - [co-body](https://www.npmjs.org/package/co-body#readme)
  16. ## Installation
  17. ```sh
  18. $ npm install body-parser
  19. ```
  20. ## API
  21. ```js
  22. var bodyParser = require('body-parser')
  23. ```
  24. ### bodyParser.json(options)
  25. Returns middleware that only parses `json`. This parser accepts any Unicode encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings.
  26. The options are:
  27. - `strict` - only parse objects and arrays. (default: `true`)
  28. - `inflate` - if deflated bodies will be inflated. (default: `true`)
  29. - `limit` - maximum request body size. (default: `<100kb>`)
  30. - `reviver` - passed to `JSON.parse()`
  31. - `type` - request content-type to parse (default: `json`)
  32. - `verify` - function to verify body content
  33. The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `json`), a mime type (like `application/json`), or a mime time with a wildcard (like `*/json`).
  34. The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.
  35. The `reviver` argument is passed directly to `JSON.parse` as the second argument. You can find more information on this argument [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).
  36. ### bodyParser.raw(options)
  37. Returns middleware that parses all bodies as a `Buffer`. This parser supports automatic inflation of `gzip` and `deflate` encodings.
  38. The options are:
  39. - `inflate` - if deflated bodies will be inflated. (default: `true`)
  40. - `limit` - maximum request body size. (default: `<100kb>`)
  41. - `type` - request content-type to parse (default: `application/octet-stream`)
  42. - `verify` - function to verify body content
  43. The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `bin`), a mime type (like `application/octet-stream`), or a mime time with a wildcard (like `application/*`).
  44. The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.
  45. ### bodyParser.text(options)
  46. Returns middleware that parses all bodies as a string. This parser supports automatic inflation of `gzip` and `deflate` encodings.
  47. The options are:
  48. - `defaultCharset` - the default charset to parse as, if not specified in content-type. (default: `utf-8`)
  49. - `inflate` - if deflated bodies will be inflated. (default: `true`)
  50. - `limit` - maximum request body size. (default: `<100kb>`)
  51. - `type` - request content-type to parse (default: `text/plain`)
  52. - `verify` - function to verify body content
  53. The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `txt`), a mime type (like `text/plain`), or a mime time with a wildcard (like `text/*`).
  54. The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.
  55. ### bodyParser.urlencoded(options)
  56. Returns middleware that only parses `urlencoded` bodies. This parser accepts only UTF-8 encoding of the body and supports automatic inflation of `gzip` and `deflate` encodings.
  57. The options are:
  58. - `extended` - parse extended syntax with the [qs](https://www.npmjs.org/package/qs#readme) module. (default: `true`)
  59. - `inflate` - if deflated bodies will be inflated. (default: `true`)
  60. - `limit` - maximum request body size. (default: `<100kb>`)
  61. - `parameterLimit` - maximum number of parameters. (default: `1000`)
  62. - `type` - request content-type to parse (default: `urlencoded`)
  63. - `verify` - function to verify body content
  64. The `extended` argument allows to choose between parsing the urlencoded data with the `querystring` library (when `false`) or the `qs` library (when `true`). The "extended" syntax allows for rich objects and arrays to be encoded into the urlencoded format, allowing for a JSON-like experience with urlencoded. For more information, please [see the qs library](https://www.npmjs.org/package/qs#readme).
  65. The `parameterLimit` argument controls the maximum number of parameters that are allowed in the urlencoded data. If a request contains more parameters than this value, a 415 will be returned to the client.
  66. The `type` argument is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme) library. This can be an extension name (like `urlencoded`), a mime type (like `application/x-www-form-urlencoded`), or a mime time with a wildcard (like `*/x-www-form-urlencoded`).
  67. The `verify` argument, if supplied, is called as `verify(req, res, buf, encoding)`, where `buf` is a `Buffer` of the raw request body and `encoding` is the encoding of the request. The parsing can be aborted by throwing an error.
  68. ### req.body
  69. A new `body` object containing the parsed data is populated on the `request` object after the middleware.
  70. ## Examples
  71. ### express/connect top-level generic
  72. This example demonstrates adding a generic JSON and urlencoded parser as a top-level middleware, which will parse the bodies of all incoming requests. This is the simplest setup.
  73. ```js
  74. var express = require('express')
  75. var bodyParser = require('body-parser')
  76. var app = express()
  77. // parse application/x-www-form-urlencoded
  78. app.use(bodyParser.urlencoded({ extended: false }))
  79. // parse application/json
  80. app.use(bodyParser.json())
  81. app.use(function (req, res) {
  82. res.setHeader('Content-Type', 'text/plain')
  83. res.write('you posted:\n')
  84. res.end(JSON.stringify(req.body, null, 2))
  85. })
  86. ```
  87. ### express route-specific
  88. This example demonstrates adding body parsers specifically to the routes that need them. In general, this is the most recommend way to use body-parser with express.
  89. ```js
  90. var express = require('express')
  91. var bodyParser = require('body-parser')
  92. var app = express()
  93. // create application/json parser
  94. var jsonParser = bodyParser.json()
  95. // create application/x-www-form-urlencoded parser
  96. var urlencodedParser = bodyParser.urlencoded({ extended: false })
  97. // POST /login gets urlencoded bodies
  98. app.post('/login', urlencodedParser, function (req, res) {
  99. if (!req.body) return res.sendStatus(400)
  100. res.send('welcome, ' + res.body.username)
  101. })
  102. // POST /api/users gets JSON bodies
  103. app.post('/api/users', jsonParser, function (req, res) {
  104. if (!req.body) return res.sendStatus(400)
  105. // create user in req.body
  106. })
  107. ```
  108. ### change content-type for parsers
  109. All the parsers accept a `type` option which allows you to change the `Content-Type` that the middleware will parse.
  110. ```js
  111. // parse various different custom JSON types as JSON
  112. app.use(bodyParser.json({ type: 'application/*+json' }))
  113. // parse some custom thing into a Buffer
  114. app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))
  115. // parse an HTML body into a string
  116. app.use(bodyParser.text({ type: 'text/html' }))
  117. ```
  118. ## License
  119. [MIT](LICENSE)
  120. [npm-image]: https://img.shields.io/npm/v/body-parser.svg?style=flat
  121. [npm-url]: https://npmjs.org/package/body-parser
  122. [travis-image]: https://img.shields.io/travis/expressjs/body-parser.svg?style=flat
  123. [travis-url]: https://travis-ci.org/expressjs/body-parser
  124. [coveralls-image]: https://img.shields.io/coveralls/expressjs/body-parser.svg?style=flat
  125. [coveralls-url]: https://coveralls.io/r/expressjs/body-parser?branch=master
  126. [downloads-image]: https://img.shields.io/npm/dm/body-parser.svg?style=flat
  127. [downloads-url]: https://npmjs.org/package/body-parser
  128. [gratipay-image]: https://img.shields.io/gratipay/dougwilson.svg?style=flat
  129. [gratipay-url]: https://www.gratipay.com/dougwilson/