nodejs body-parser 解析post数据

安装

$ npm install body-parser

API

var bodyPaeser =require('body-parser')

可以通过body-parser 对象创建中间件,当接收到客户端请求时所有的中间件都会给req.body 添加属性,请求体为空,则解析为空{} (或者出现错误)。

bodyParser.json(options)

中间件只会解析 json ,允许请求提任意Unicode编码支持 gzip 和 deflate 编码。

options

一个对象,有以下属性

inflate

默认为false,true->压缩的请求体会被解压,false->压缩的请求提不被解压。

limit

控制请求体最大大小,默认为100kb,当为数字时会转换为bytes,当为字符串时,value值会通过 bytes库 转换为字节大小。

reviver

此选项会通过JSON.parse直接传给其第二个参数。

strict

默认为true,当为true时只接受数组和对象,当为false时会接受任何JSON.parse 能接受的。

type

type 选项用来决定中间件要解析媒体类型。选项可以是一个函数或者是字符串。当为字符串时,可以直接通过type-is 库直接传递给选项,字符串也可以为一个扩展名(例如json)、mime 类型(application/json、/ 、*/json)。当为函数时:默认为application/json。

verify

verify选项,若缺失则为一个函数function(req,res,buf,encoding),buf为一个Buffer。

bodyParse.raw(option)

将请求体内容作为Buffer来处理,并返回。支持gzip deflate 压缩。

inflate

limit

type

verify

bodyParser.text(option)

将请求提内容作为字符串来处理,并返回。支持gzip deflate 压缩。

defaultCharset

若请求头未设置Content-Type则默认为utf8

inflate

type

verify

bodyParser.urlencoded(option)

中间件只解析urlencoded 请求体,并返回,只支持UTF-8编号文本,支持gzip deflate 压缩。

extend

ture->使用queryString库(默认) false->使用qs库。

limit

parameterlimit

指定parameters最长长度,默认1000

type

verify

举例:

 1 const express=require('express');
 2 const bodyParser=require('body-parser' 3 
 4 var server=express();
 5 server.listen(8080 6 
 7 server.use(bodyParser.urlencoded({
 8   extended: false,//扩展模式
 9   limit:    2*1024*1024           限制-2M
10 }));
11 
12 server.use('/',1)">function (req,res){
13   console.log(req.body); POST
14   req.query   GET
15  req.body    POST
16 });

html代码:

<form action="http://localhost:8080" method="post">
      用户:<input type="text" name="user" /><br>
      密码:<input type="password" name="pass" /><br>
<input type="submit" value="提交" >

 

相关文章

这篇文章主要介绍“基于nodejs的ssh2怎么实现自动化部署”的...
本文小编为大家详细介绍“nodejs怎么实现目录不存在自动创建...
这篇“如何把nodejs数据传到前端”文章的知识点大部分人都不...
本文小编为大家详细介绍“nodejs如何实现定时删除文件”,内...
这篇文章主要讲解了“nodejs安装模块卡住不动怎么解决”,文...
今天小编给大家分享一下如何检测nodejs有没有安装成功的相关...