日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當(dāng)前位置:首頁 > 科技  > 軟件

Body-Parser:一個格式化請求體數(shù)據(jù)的 Express 三方庫

來源: 責(zé)編: 時間:2024-02-29 14:43:45 180觀看
導(dǎo)讀body-parser 是 Express 中用于格式化請求體數(shù)據(jù)的一個三方庫。以下是一個 body-parser 的常用使用案例。const express = require('express')const bodyParser = require('body-parser')const app = express()// par

body-parser 是 Express 中用于格式化請求體數(shù)據(jù)的一個三方庫。9sR28資訊網(wǎng)——每日最新資訊28at.com

以下是一個 body-parser 的常用使用案例。9sR28資訊網(wǎng)——每日最新資訊28at.com

const express = require('express')const bodyParser = require('body-parser')const 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))})

以上代碼設(shè)置的含義是:項目中所有 Content-Type 是 "application/x-www-form-urlencoded" 或 "application/json" 請求數(shù)據(jù)都會經(jīng)過 body-parser 中間件的處理,并設(shè)置到 req.body 屬性上。9sR28資訊網(wǎng)——每日最新資訊28at.com

下面就來詳細(xì)介紹 body-parser 的安裝和使用。9sR28資訊網(wǎng)——每日最新資訊28at.com

安裝

$ npm install body-parser

使用也比較簡單。9sR28資訊網(wǎng)——每日最新資訊28at.com

const bodyParser = require('body-parser')

我們是通過調(diào)用 bodyParser 對象的方法來獲取不同 Content-Type 的數(shù)據(jù)處理能力的。下面就來介紹。9sR28資訊網(wǎng)——每日最新資訊28at.com

API

bodyParser 上一共提供了 4 個方法來使用,分別對應(yīng) 4 種不同類型的請求體數(shù)據(jù)。9sR28資訊網(wǎng)——每日最新資訊28at.com

  1. bodyParser.json([options])
  2. bodyParser.urlencoded([options])
  3. bodyParser.text([options])
  4. bodyParser.raw([options])

其中前 2 種是我們最常使用的。接下來分別介紹。9sR28資訊網(wǎng)——每日最新資訊28at.com

bodyParser.json([options])

返回能解析 json 數(shù)據(jù)的中間件,默認(rèn)匹配 Content-Type 值為 "application/json"(可以通過 options.type 進行控制)。接受任何 Unicode  編碼的請求體數(shù)據(jù),而且還支持 gzip 和 deflate 壓縮算法的自動解壓。9sR28資訊網(wǎng)——每日最新資訊28at.com

解析好的請求體數(shù)據(jù)會放在 req.body 屬性上,無請求體數(shù)據(jù)則返回一個空對象({})。9sR28資訊網(wǎng)——每日最新資訊28at.com

Options 選項

這是一個可選參數(shù),不傳入則使用默認(rèn)選項。其他 3 個方法與此類似,在此講 1 遍后,如果沒有特殊情況,后面就不再贅述了。9sR28資訊網(wǎng)——每日最新資訊28at.com

  • type:默認(rèn)值是 "application/json",表示這個中間件默認(rèn)匹配的 Content-Type 是 "application/json",這個字段除了接收字符串,還支持接收對象和數(shù)組。你也可以自定義,比如設(shè)置成 "*/json"(內(nèi)部是使用 type-is[1] 做匹配的)
  • inflate:是否開啟自動解壓請求體數(shù)據(jù)的能力,默認(rèn)為 true(目前支持 支持 gzip 和 deflate 2 中壓縮算法)
  • limit:控制最大請求正文大小,否則報錯。默認(rèn)值是 '100kb'(內(nèi)部傳遞給 bytes[2] 庫將其解析成字節(jié)數(shù)進行判斷處理)
  • reviver:body-parser 內(nèi)部默認(rèn)使用 JSON.parse() 方法將字符串格式化成對象設(shè)置到 req.body 上。這個參數(shù)就是傳遞給 JSON.parse() 方法的第 2 個參數(shù),用來自定義解析行為。搞不清楚的同學(xué)可以看一下 MDN 上 JSON.parse() 的使用文檔[3]
  • strict:默認(rèn)為 true,用來控制內(nèi)部 JSON.parse() 所接收的字符串是否只能是對象和數(shù)組。設(shè)置成 false 時表示可以接受任意類型的字符串?dāng)?shù)據(jù)

bodyParser.urlencoded([options])

默認(rèn)處理 Content-Type 值為 "application/x-www-form-urlencoded" 的請求體數(shù)據(jù)。9sR28資訊網(wǎng)——每日最新資訊28at.com

.urlencoded() 中間件還提供了一個布爾參數(shù) urlencoded,用來決定使用哪一個查詢解析庫來處理請求參數(shù)。9sR28資訊網(wǎng)——每日最新資訊28at.com

  • urlencoded為 false 時,內(nèi)部使用 querystring 庫解析
  • urlencoded為 true 時,內(nèi)部使用 qs 庫解析

qs 與 querystring 的區(qū)別在于:querystring 只能解析簡單 key-value 對,qs 則支持嵌套對象的 key-value 對的解析,后者功能會更加強大。9sR28資訊網(wǎng)——每日最新資訊28at.com

Options 選項

  • urlencoded:以上已介紹過。默認(rèn)為 true(即使用 qs 解析參數(shù)),不過已被棄。官方推薦顯式傳入一個值,這就需要你了解 qs 和 querystring 之間的區(qū)別
  • parameterLimit:控制 URL 編碼數(shù)據(jù)中允許的最大參數(shù)數(shù)量,默認(rèn)是 1000。多于這個值,會向客戶端返回 413 響應(yīng)碼

bodyParser.text([options])

默認(rèn)處理 Content-Type 值為 "text/plain" 的請求體數(shù)據(jù)。9sR28資訊網(wǎng)——每日最新資訊28at.com

Options 選項

  • defaultCharset:如果請求頭中的 Content-Type 頭中沒有指定字符集,則使用這個字段所指定的文本內(nèi)容的默認(rèn)字符集。默認(rèn)為 "utf-8"。

bodyParser.raw([options])

默認(rèn)處理 Content-Type 值為 "application/octet-stream" 的請求體數(shù)據(jù)。9sR28資訊網(wǎng)——每日最新資訊28at.com

不支持 multipart/form-data

body-parser 并不支持處理 Content-Type 是 "multipart/form-data" 的請求體數(shù)據(jù)。如果你有這方面的需求,可以參照下面的庫進行選擇。9sR28資訊網(wǎng)——每日最新資訊28at.com

  • busboy[4] 和 connect-busboy[5]
  • multiparty[6] 和 connect-multiparty[7]
  • formidable[8]
  • multer[9]

express 已內(nèi)置 body-parser

express 從 v4.17.0 開始[10],已全面內(nèi)置了 body-parser 功能,你直接可以通過 express().json() / express().raw()/express().text()/ express().urlencoded() 4 個 API。9sR28資訊網(wǎng)——每日最新資訊28at.com

在內(nèi)部,這 4 方法其實是 body-parser 借著 express 暴露出來[11]的。也就是說項目中你無需安裝 body-parser 依賴了。9sR28資訊網(wǎng)——每日最新資訊28at.com

const bodyParser = require('body-parser')/** * Expose middleware */exports.json = bodyParser.jsonexports.query = require('./middleware/query');exports.raw = bodyParser.rawexports.text = bodyParser.textexports.urlencoded = bodyParser.urlencoded

總結(jié)

本文介紹了 Express 中用于解析請求體數(shù)據(jù)的中間件 body-parser。 其實 Express 在 v4.17.0 中已完整內(nèi)置了 body-parser 的能力,你可以通過 express() 上的 .json()/.urlencoded()/.text()/.raw() 方法訪問到。9sR28資訊網(wǎng)——每日最新資訊28at.com

就介紹到這里了,希望本文對你有所幫助,感謝閱讀,再見。9sR28資訊網(wǎng)——每日最新資訊28at.com

參考資料

[1]type-is: https://www.npmjs.org/package/type-is#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[2]bytes: https://www.npmjs.com/package/bytes9sR28資訊網(wǎng)——每日最新資訊28at.com

[3]MDN 上 JSON.parse() 的使用文檔: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter9sR28資訊網(wǎng)——每日最新資訊28at.com

[4]busboy: https://www.npmjs.org/package/busboy#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[5]connect-busboy: https://www.npmjs.org/package/connect-busboy#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[6]multiparty: https://www.npmjs.org/package/multiparty#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[7]connect-multiparty: https://www.npmjs.org/package/connect-multiparty#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[8]formidable: https://www.npmjs.org/package/formidable#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[9]multer: https://www.npmjs.org/package/multer#readme9sR28資訊網(wǎng)——每日最新資訊28at.com

[10]從 v4.17.0 開始: https://expressjs.com/en/4x/api.html#express.json9sR28資訊網(wǎng)——每日最新資訊28at.com

[11]body-parser 借著 express 暴露出來: https://github.com/expressjs/express/blob/4.18.2/lib/express.js#L789sR28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-75357-0.htmlBody-Parser:一個格式化請求體數(shù)據(jù)的 Express 三方庫

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 詳解滲透測試和漏洞掃描的開源自動化解決方案

下一篇: 如何在Rust中操作JSON,你學(xué)會了嗎?

標(biāo)簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 惠水县| 沙坪坝区| 嘉鱼县| 凤冈县| 濮阳县| 兴业县| 遵化市| 中西区| 九江县| 莱州市| 张家港市| 隆德县| 岑巩县| 六安市| 清原| 南漳县| 盘山县| 天镇县| 秭归县| 顺平县| 阜阳市| 涟源市| 新龙县| 高清| 岱山县| 古丈县| 昌乐县| 乡宁县| 九台市| 满洲里市| 平顶山市| 济阳县| 正阳县| 汶上县| 云龙县| 格尔木市| 岚皋县| 禹城市| 融水| 建昌县| 清流县|