body-parser小解
body-parser用来解析http请求体,对不同的content-type有不同的处理方式,
首先介绍一下常见的四种Content-Type:
1.application/x-www-form-urllencodes form表单提交
2.application/json 提交json格式的数据
3.text/xml 提交xml格式的数据
4.multipart/form-data 文件提交
body-parser对body也有四种处理方式:
1.bodyParser.urlencoded(options)
2.bodyParser.json(options)
3.bodyParser.text(options)
4.bodyParser.raw(options)
解析实质: 解析原有的req.body,解析成功后用解析结果覆盖原有的req.body,解析失败则为{}
urlencoded小解
常见的使用方法为bodyParser.urlencoded({extened: false})
解析: querystring是node内建的对象,用来字符串化对象或解析字符串,qs为一个querystring库,在其基础上添加更多的功能和安全性,
extened为true时表示再额外加上qs,一般不需要
常见用法:
1、底层中间件用法:这将拦截和解析所有的请求;也即这种用法是全局的。
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))
})
express的use方法调用body-parser实例;且use方法没有设置路由路径;这样的body-parser实例就会对该app所有的请求进行拦截和解析。
2、特定路由下的中间件用法:这种用法是针对特定路由下的特定请求的,只有请求该路由时,中间件才会拦截和解析该请求;也即这种用法是局部的;也是最常用的一个方式。
var express = require('express')
var bodyParser = require('body-parser') var app = express() // create application/json parser
var jsonParser = bodyParser.json() // create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false }) // POST /login gets urlencoded bodies
app.post('/login', urlencodedParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
res.send('welcome, ' + req.body.username)
}) // POST /api/users gets JSON bodies
app.post('/api/users', jsonParser, function (req, res) {
if (!req.body) return res.sendStatus(400)
// create user in req.body
})
3、设置Content-Type 属性;用于修改和设定中间件解析的body类容类型。
// 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' }));
body-parser小解的更多相关文章
- [LeetCode] Mini Parser 迷你解析器
Given a nested list of integers represented as a string, implement a parser to deserialize it. Each ...
- Log Parser 2.2 分析 IIS 日志
1,安装Log Parser 2.2 https://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=24659 ...
- [译文]选择使用正确的 Markdown Parser
以下客座文章由Ray Villalobos提供.在这篇文章中Ray将要去探索很多种不同的Markdown语法.所有的这些MarkDown变种均提供了不同的特性,都超越传统的Markdown语法,却又相 ...
- InnoDB全文索引:N-gram Parser【转】
本文来自:http://mysqlserverteam.com/innodb%E5%85%A8%E6%96%87%E7%B4%A2%E5%BC%95%EF%BC%9An-gram-parser/ In ...
- Warning: simplexml_load_string(): Entity: line 432: parser error : EntityRef: expecting ';'
Warning: simplexml_load_string(): Entity: line 432: parser error : EntityRef: expecting ';' characte ...
- Lex&Yacc Parser错误发生后再次parser之前恢复初始状态
使用lex yacc 对文件进行parser时,如果文件内容有错,parser报错,然后你修改了文件,再次读入文件进行parser,如果你不是重启程序进行parser,那就需要对做些处理了. &quo ...
- 为sproto手写了一个python parser
这是sproto系列文章的第三篇,可以参考前面的<为sproto添加python绑定>.<为python-sproto添加map支持>. sproto是云风设计的序列化协议,用 ...
- Python html.parser库学习小结
分类路径:/Datazen/DataMining/Crawler/ 前段时间,一朋友让我做个小脚本,抓一下某C2C商城上竞争对手的销售/价格数据,好让他可以实时调整自己的营销策略.自己之前也有过写 ...
- 用Log Parser Studio分析IIS日志
发现一个强大的图形化IIS日志分析工具——Log Parser Studio,下面分享一个实际操作案例. 1. 安装Log Parser Studio a) 需要先安装Log Parser,下载地址: ...
随机推荐
- 项目实战:JSP应用开发_接口:接口的实现
在类的声明中使用implements关键字来实现接口,一个类可以同时实现多个接口,各接口间用“,”隔开. class classname implements interfacename{ //重 ...
- iOS 创建多个button实现点击改变背景颜色
工程中需要实现与UISegmentedControl效果相似的一排一共十个button,如下图.但是SegmentedControl修改不太方便,就用button替代, 循环创建十个button,点击 ...
- array_sum的用法
众所周知,PHP中函数是功能很强大的,那么今天就说下array_sum的功能吧. 函数功能:返回数组中所有值的和. 举例: <?php $a = array(1,2); $b = array_s ...
- 如何获取继承中泛型T的类型
@SuppressWarnings("unchecked") public void testT() { clazz = (Class<T>)( (Parameteri ...
- Ubuntu下MongoDB的安装和使用
本博文介绍了MongoDB,并详细指引读者在Ubuntu下MongoDB的安装和使用.本教程在Ubuntu14.04下测试通过.(2017.09.07) 安装MongoDB MongoDB安装很简单, ...
- mybatis if test 相等的情况怎样动态拼接sql
今天程序须要依据前台的传过来的状态推断在数据库里是取 where a>b 还是 a<b 还是 a=0 的情况 搞了一下午最后试了下 在if 里面拼接 #{status}=#{statu ...
- EasyNVR智能云终端硬件使用说明(EasyNVR无插件直播服务硬件的具体使用方法)
问题背景 随着EasyNVR硬件版本(EasyNVR硬件云终端)的发布不少客户选择了EasyNVR云终端作为产品选择,在客户收到EasyNVR云终端的时候肯定都有一个疑问,那就是如何使用手头上的这个小 ...
- spark2.0.2基于hadoop2.4搭建分布式集群
一.Scala安装 因为spark的版本原因,所以Scala我用的2.11.7. 下载目录http://www.scala-lang.org/download/ 拷贝到要安装的地址,我的地址是/usr ...
- coursera 《现代操作系统》 -- 第五周 同步机制(2)
分清紧急等待队列与条件等待队列(c 链) 条件等待队列:但是进入管程的这个进程可能由于对资源的操作的过程中发现条件不成熟, 那么它就不能够继续对资源进行相应的操作. 我们以生产者. 消费者为例. 如果 ...
- 如何定义 match 常量?
namespace MathConstants { const double E = 2.71828182845904523536; // e const double LOG2E = 1.44269 ...