EXPRESS bodyParser


API

请求体的四种解析方式

  1. bodyParser.json(options): 解析json数据
  2. bodyParser.raw(options): 解析二进制格式(Buffer流数据)
  3. bodyParser.text(options): 解析文本数据
  4. bodyParser.urlencoded(options): 解析UTF-8的编码的数据。

bodyParser变量是对中间件的引用。请求体解析后,解析值都会被放到req.body属性,内容为空时是一个{}空对象。

bodyParser.json(options)返回一个仅解析json格式数据的中间件 options的可选项

  1. inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
  2. limit - 设置请求的最大数据量。默认为’100kb’
  3. reviver - 传递给JSON.parse()方法的第二个参数,详见JSON.parse()
  4. strict - 设置为true时,仅会解析Array和Object两种格式;设置为false会解析所有JSON.parse支持的格式。默认为true
  5. type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/json。
  6. verify - 这个选项仅在verify(req, res, buf, encoding)时受支持

bodyParser.raw(options)返回一个将所有数据做为Buffer格式处理的中间件.其后的所有的req.body中将会是一个Buffer值。 options的可选项

  1. inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
  2. limit - 设置请求的最大数据量。默认为’100kb’
  3. type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
  4. verify - 这个选项仅在verify(req, res, buf, encoding)时受支持

bodyParser.text(options) 解析文本格式 返回一个仅处理字符串格式处理的中间件。其后的所有的req.body中将会是一个字符串值。 options选项

  1. defaultCharset - 如果Content-Type后没有指定编码时,使用此编码。默认为’utf-8’
  2. inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
  3. limit - 设置请求的最大数据量。默认为’100kb’
  4. type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
  5. verify - 这个选项仅在verify(req, res, buf, encoding)时受支持

bodyParser.urlencoded(options) 解析UTF-8的编码的数据。返回一个处理urlencoded数据的中间件。 options选项

  1. extended - 当设置为false时,会使用querystring库解析URL编码的数据;当设置为true时,会使用qs库解析URL编码的数据。后没有指定编码时,使用此编码。默认为true
  2. inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
  3. limit - 设置请求的最大数据量。默认为’100kb’
  4. parameterLimit - 用于设置URL编码值的最大数据。默认为1000
  5. type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
  6. verify - 这个选项仅在verify(req, res, buf, encoding)时受支持

使用方式

var express= require('express');
var bodyParser = require('body-parser');

var app=new express();

//创建application/json解析
var jsonParser=bodyParser.json();

//创建application/x-www-form-urlencoded解析
var urlencodeParser = bodyParser.urlencoded({extended:false});

app.use(urlencodeParser);//使用中间件

app.get('/',function(req,res){
    res.sendfile(__dirname+'/index.html');
});

app.post('/login',function(req,res){
    if(!req.body) return res.sendStatus(400);
    res.send('welcome,'+req.body.username);
    console.log(req.body);//在终端打印请求体
});

app.listen(3000);

必须指定content-type

Express's bodyParser only parses the incoming data, if the content-type is set to either of the following:

  1. application/x-www-form-urlencoded
  2. application/json
  3. multipart/form-data


一般使用:

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use(function (req, res) {
  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))
})

Change accepted type for parsers

All the parsers accept a type option which allows you to change the Content-Type that the middleware will parse.

var express = require('express')
var bodyParser = require('body-parser')

var app = express()

// parse various different custom JSON types as JSON
app.use(bodyParser.json({ type: 'application/*+json' }))

// parse some custom thing into a Buffer
app.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))

// parse an HTML body into a string
app.use(bodyParser.text({ type: 'text/html' }))



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值