API
请求体的四种解析方式
- bodyParser.json(options): 解析json数据
- bodyParser.raw(options): 解析二进制格式(Buffer流数据)
- bodyParser.text(options): 解析文本数据
- bodyParser.urlencoded(options): 解析UTF-8的编码的数据。
bodyParser
变量是对中间件的引用。请求体解析后,解析值都会被放到req.body
属性,内容为空时是一个{}空对象。
bodyParser.json(options)
返回一个仅解析json格式数据的中间件
options的可选项
- inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
- limit - 设置请求的最大数据量。默认为’100kb’
- reviver - 传递给JSON.parse()方法的第二个参数,详见JSON.parse()
- strict - 设置为true时,仅会解析Array和Object两种格式;设置为false会解析所有JSON.parse支持的格式。默认为true
- type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/json。
- verify - 这个选项仅在verify(req, res, buf, encoding)时受支持
bodyParser.raw(options)
返回一个将所有数据做为Buffer
格式处理的中间件.其后的所有的req.body
中将会是一个Buffer
值。
options的可选项
- inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
- limit - 设置请求的最大数据量。默认为’100kb’
- type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
- verify - 这个选项仅在verify(req, res, buf, encoding)时受支持
bodyParser.text(options)
解析文本格式 返回一个仅处理字符串格式处理的中间件。其后的所有的req.body
中将会是一个字符串值。
options选项
- defaultCharset - 如果Content-Type后没有指定编码时,使用此编码。默认为’utf-8’
- inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
- limit - 设置请求的最大数据量。默认为’100kb’
- type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
- verify - 这个选项仅在verify(req, res, buf, encoding)时受支持
bodyParser.urlencoded(options)
解析UTF-8的编码的数据。返回一个处理urlencoded
数据的中间件。
options选项
- extended - 当设置为false时,会使用querystring库解析URL编码的数据;当设置为true时,会使用qs库解析URL编码的数据。后没有指定编码时,使用此编码。默认为true
- inflate - 设置为true时,deflate压缩数据会被解压缩;设置为true时,deflate压缩数据会被拒绝。默认为true。
- limit - 设置请求的最大数据量。默认为’100kb’
- parameterLimit - 用于设置URL编码值的最大数据。默认为1000
- type - 该选项用于设置为指定MIME类型的数据使用当前解析中间件。这个选项可以是一个函数或是字符串,当是字符串是会使用type-is来查找MIMI类型;当为函数是,中间件会通过fn(req)来获取实际值。默认为application/octet-stream。
- 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:
application/x-www-form-urlencoded
application/json
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' }))
EXPRESS bodyParser
最新推荐文章于 2024-03-29 05:31:16 发布