bodyParser.json(options) options可选 , 这个方法返回一个仅仅用来解析json格式的中间件。这个中间件能接受任何body中任何Unicode编码的字符。支持自动的解析gzip和 zlib。
bodyParser.urlencoded(options) options可选,这个方法也返回一个中间件,这个中间件用来解析body中的urlencoded字符, 只支持utf-8的编码的字符。同样也支持自动的解析gzip和 zlib。
链接:https://www.zhihu.com/question/36962099/answer/125337339
bodyParser.json是用来解析json数据格式的。bodyParser.urlencoded则是用来解析我们通常的form表单提交的数据,也就是请求头中包含这样的信息: Content-Type: application/x-www-form-urlencoded
常见的四种Content-Type类型:
-
application/x-www-form-urlencoded
常见的form提交 -
multipart/form-data
文件提交 -
application/json
提交json格式的数据 -
text/xml
提交xml格式的数据
bodyParser.urlencoded
模块用于解析req.body的数据,解析成功后覆盖原来的req.body,如果解析失败则为 {}
。该模块有一个属性extended,官方介绍如下:
The extended option allows to choose between parsing the URL-encoded data with the querystring library (when false) or the qs library (when true). Defaults to true, but using the default has been deprecated.
大致的意思就是:extended选项允许配置使用querystring(false)或qs(true)来解析数据,默认值是true,但这已经是不被赞成的了。