EXPRESS bodyParser

来源:互联网 发布:java 获取文件路径 编辑:程序博客网 时间:2024/06/16 12:21

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-urlencodedapp.use(bodyParser.urlencoded({ extended: false }))// parse application/jsonapp.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 JSONapp.use(bodyParser.json({ type: 'application/*+json' }))// parse some custom thing into a Bufferapp.use(bodyParser.raw({ type: 'application/vnd.custom-type' }))// parse an HTML body into a stringapp.use(bodyParser.text({ type: 'text/html' }))