# body-parser
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
[![Build Status][travis-image]][travis-url]
[![Test Coverage][coveralls-image]][coveralls-url]
[![Gratipay][gratipay-image]][gratipay-url]
Node.js body parsing middleware.
Parse incoming request bodies in a middleware before your handlers, availabe
under the `req.body` property.
[Learn about the anatomy of an HTTP transaction in Node.js](https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/).
_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:
* [busboy](https://www.npmjs.org/package/busboy#readme) and
[connect-busboy](https://www.npmjs.org/package/connect-busboy#readme)
* [multiparty](https://www.npmjs.org/package/multiparty#readme) and
[connect-multiparty](https://www.npmjs.org/package/connect-multiparty#readme)
* [formidable](https://www.npmjs.org/package/formidable#readme)
* [multer](https://www.npmjs.org/package/multer#readme)
This module provides the following parsers:
* [JSON body parser](#bodyparserjsonoptions)
* [Raw body parser](#bodyparserrawoptions)
* [Text body parser](#bodyparsertextoptions)
* [URL-encoded form body parser](#bodyparserurlencodedoptions)
Other body parsers you might be interested in:
- [body](https://www.npmjs.org/package/body#readme)
- [co-body](https://www.npmjs.org/package/co-body#readme)
## Installation
```sh
$ npm install body-parser
```
## API
```js
var bodyParser = require('body-parser')
```
The `bodyParser` object exposes various factories to create middlewares. All
middlewares will populate the `req.body` property with the parsed body, or an
empty object (`{}`) if there was no body to parse (or an error was returned).
The various errors returned by this module are described in the
[errors section](#errors).
### bodyParser.json(options)
Returns middleware that only parses `json`. This parser accepts any Unicode
encoding of the body and supports automatic inflation of `gzip` and `deflate`
encodings.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`).
#### Options
The `json` function takes an option `options` object that may contain any of
the following keys:
##### inflate
When set to `true`, then deflated (compressed) bodies will be inflated; when
`false`, deflated bodies are rejected. Defaults to `true`.
##### limit
Controls the maximum request body size. If this is a number, then the value
specifies the number of bytes; if it is a string, the value is passed to the
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
to `'100kb'`.
##### reviver
The `reviver` option 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).
##### strict
When set to `true`, will only accept arrays and objects; when `false` will
accept anything `JSON.parse` accepts. Defaults to `true`.
##### type
The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `json`), a mime type (like
`application/json`), or a mime type with a wildcard (like `*/*` or `*/json`).
If a function, the `type` option is called as `fn(req)` and the request is
parsed if it returns a truthy value. Defaults to `application/json`.
##### verify
The `verify` option, 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.
### bodyParser.raw(options)
Returns middleware that parses all bodies as a `Buffer`. This parser
supports automatic inflation of `gzip` and `deflate` encodings.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This will be a `Buffer` object
of the body.
#### Options
The `raw` function takes an option `options` object that may contain any of
the following keys:
##### inflate
When set to `true`, then deflated (compressed) bodies will be inflated; when
`false`, deflated bodies are rejected. Defaults to `true`.
##### limit
Controls the maximum request body size. If this is a number, then the value
specifies the number of bytes; if it is a string, the value is passed to the
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
to `'100kb'`.
##### type
The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `bin`), a mime type (like
`application/octet-stream`), or a mime type with a wildcard (like `*/*` or
`application/*`). If a function, the `type` option is called as `fn(req)`
and the request is parsed if it returns a truthy value. Defaults to
`application/octet-stream`.
##### verify
The `verify` option, 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.
### bodyParser.text(options)
Returns middleware that parses all bodies as a string. This parser supports
automatic inflation of `gzip` and `deflate` encodings.
A new `body` string containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This will be a string of the
body.
#### Options
The `text` function takes an option `options` object that may contain any of
the following keys:
##### defaultCharset
Specify the default character set for the text content if the charset is not
specified in the `Content-Type` header of the request. Defaults to `utf-8`.
##### inflate
When set to `true`, then deflated (compressed) bodies will be inflated; when
`false`, deflated bodies are rejected. Defaults to `true`.
##### limit
Controls the maximum request body size. If this is a number, then the value
specifies the number of bytes; if it is a string, the value is passed to the
[bytes](https://www.npmjs.com/package/bytes) library for parsing. Defaults
to `'100kb'`.
##### type
The `type` option is used to determine what media type the middleware will
parse. This option can be a function or a string. If a string, `type` option
is passed directly to the [type-is](https://www.npmjs.org/package/type-is#readme)
library and this can be an extension name (like `txt`), a mime type (like
`text/plain`), or a mime type with a wildcard (like `*/*` or `text/*`).
If a function, the `type` option is called as `fn(req)` and the request is
parsed if it returns a truthy value. Defaults to `text/plain`.
##### verify
The `verify` option, 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.
### bodyParser.urlencoded(options)
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.
A new `body` object containing the parsed data is populated on the `request`
object after the middleware (i.e. `req.body`). This object will contain
key-value pairs, where the value can be a string or array (when `extended` is
`false`), or any type (when `extended` is `true`).
#### Options
The `urle
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
实时信令网络(主要针对社交物联网,_远程操控,_娃娃机,捕鱼机,宠物喂养机,机器人,无人机等)_rtn.zip (986个子文件)
configure.ac 7KB
Makefile.am 6KB
Makefile.am 3KB
Makefile.am 3KB
Dockerfile.build 2KB
Cakefile 624B
babel.cfg 86B
ChangeLog 240B
channel-server 39B
ipaddr.test.coffee 13KB
ipaddr.coffee 12KB
COPYING 34KB
bootstrap.min.css 109KB
main.min.css 51KB
font-awesome.min.css 19KB
csp.min.css 996B
style.css 111B
style.css 32B
Dockerfile 3KB
comments.ejs 387B
rmWhitespace.ejs 353B
menu_preprocessor.ejs 251B
menu.ejs 222B
index.ejs 222B
menu_var.ejs 183B
newlines.mixed.ejs 131B
no.semicolons.ejs 131B
literal.ejs 114B
include-abspath.ejs 114B
space-and-tab-slurp.ejs 98B
include.ejs 98B
newlines.ejs 90B
no.newlines.ejs 90B
error.ejs 83B
include_preprocessor.ejs 80B
messed.ejs 72B
error.ejs 63B
include_preprocessor.css.ejs 62B
include.css.ejs 60B
no.newlines.error.ejs 47B
consecutive-tags.ejs 47B
include-simple.ejs 44B
include_preprocessor_cache.ejs 43B
double-quote.ejs 41B
item.ejs 39B
fail.ejs 35B
bom.ejs 35B
single-quote.ejs 34B
menu-item.ejs 33B
include_cache.ejs 33B
user-no-with.ejs 28B
pet.ejs 25B
user.ejs 21B
hello-world.ejs 20B
with-context.ejs 16B
para.ejs 11B
backslash.ejs 5B
fontawesome-webfont.eot 71KB
.eslintignore 5B
.eslintignore 5B
.eslintrc 477B
me-wechat.gif 115KB
.gitignore 466B
.gitignore 6B
users.go 15KB
main.go 14KB
session.go 9KB
api_test.go 9KB
bus_lmq.go 8KB
bus_manager.go 7KB
room_manager.go 7KB
api.go 6KB
roomworker.go 6KB
hub.go 6KB
config.go 5KB
pipeline_manager.go 5KB
connection.go 5KB
pipeline.go 5KB
session_manager.go 5KB
data.go 5KB
room_manager_test.go 5KB
roomworker_test.go 4KB
imagecache.go 4KB
tickets.go 4KB
natsconnection.go 4KB
tls.go 4KB
sessions.go 4KB
buffercache.go 3KB
handler_sandbox.go 3KB
config.go 2KB
pipelines.go 2KB
stats_manager.go 2KB
client.go 2KB
stats.go 2KB
handle_chat.go 2KB
handler_room.go 2KB
user.go 2KB
handler_ws.go 2KB
randomstring.go 2KB
tickets_test.go 2KB
共 986 条
- 1
- 2
- 3
- 4
- 5
- 6
- 10
资源评论
好家伙VCC
- 粉丝: 2410
- 资源: 9138
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Linux环境下编译图像查看工具View-image的Ubuntu实践教程
- 基于STM32视频监控系统源码(高分毕业设计项目).zip
- 利用 JavaScript 的 Date 对象获取当前时间,在网页上展示实时数字时钟的源码
- 实验八、Linux gcc源码编译实验(二).doc
- 基于STM32的视频监控系统项目源码(高分毕业设计项目)
- Linux环境下的Opencv编译运行指南:基于Ubuntu系统的图像显示程序实现
- lab8-课外扩展任务-编译opencv程序.7z
- 八月最新完美运营版 PHP在线客服系统服务器完整打包
- 实验九、编写Linux Shell脚本.doc
- 中文字体用于matplotlib
- 实验九+、Linux Shell变量与环境配置.doc
- web服务调用代码,C#实现
- lab9-Linux脚本.7z
- 1736408193931885_110_f0c21c4061d20975109eb2c4bcb0aa89.apk
- 一个 用 HTML 构建页面,JavaScript 处理下拉菜单联动的源码
- 实验十、Linux网络参数配置与网络服务管理.doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功