EXPRESS bodyParser
来源:互联网 发布:java 获取文件路径 编辑:程序博客网 时间:2024/06/16 12:21
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-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' }))
阅读全文
0 0
- EXPRESS bodyParser
- express中间件--bodyparser
- 理解express中间件bodyparser
- express.bodyParser文件路径设置
- nodejs 使用app.use(express.bodyParser()); 出行异常----解决方法
- Most middleware (like bodyParser) is no longer bundled with Express
- bodyParser中间件的研究
- bodyparser的作用
- Error: Most middleware (like bodyParser) ...
- express
- express
- Express
- express
- Express
- express
- Express
- express
- Express
- Hololens入门之拍照编辑
- 二分法求方程的根
- 火狐浏览器,改变你的浏览体验
- 博客开始打卡
- Spring MVC
- EXPRESS bodyParser
- CF835B-The number on the board
- jsp页面设置了IE版本Edge但是不生效
- Hi3519-pinctrl驱动开发②创建mapping table
- 值得学习的c语言开源项目
- word审阅功能、查看word文档的好姿势
- 【Java】Servlet基础及应用
- HDU 1465 不容易系列之一
- 【POJ 2407】Relatives(欧拉函数-水题)