body-parser 是 Express 中用于格式化請求體數(shù)據(jù)的一個三方庫。
以下是一個 body-parser 的常用使用案例。
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 屬性上。
下面就來詳細(xì)介紹 body-parser 的安裝和使用。
$ npm install body-parser
使用也比較簡單。
const bodyParser = require('body-parser')
我們是通過調(diào)用 bodyParser 對象的方法來獲取不同 Content-Type 的數(shù)據(jù)處理能力的。下面就來介紹。
bodyParser 上一共提供了 4 個方法來使用,分別對應(yīng) 4 種不同類型的請求體數(shù)據(jù)。
其中前 2 種是我們最常使用的。接下來分別介紹。
返回能解析 json 數(shù)據(jù)的中間件,默認(rèn)匹配 Content-Type 值為 "application/json"(可以通過 options.type 進行控制)。接受任何 Unicode 編碼的請求體數(shù)據(jù),而且還支持 gzip 和 deflate 壓縮算法的自動解壓。
解析好的請求體數(shù)據(jù)會放在 req.body 屬性上,無請求體數(shù)據(jù)則返回一個空對象({})。
這是一個可選參數(shù),不傳入則使用默認(rèn)選項。其他 3 個方法與此類似,在此講 1 遍后,如果沒有特殊情況,后面就不再贅述了。
默認(rèn)處理 Content-Type 值為 "application/x-www-form-urlencoded" 的請求體數(shù)據(jù)。
.urlencoded() 中間件還提供了一個布爾參數(shù) urlencoded,用來決定使用哪一個查詢解析庫來處理請求參數(shù)。
qs 與 querystring 的區(qū)別在于:querystring 只能解析簡單 key-value 對,qs 則支持嵌套對象的 key-value 對的解析,后者功能會更加強大。
默認(rèn)處理 Content-Type 值為 "text/plain" 的請求體數(shù)據(jù)。
默認(rèn)處理 Content-Type 值為 "application/octet-stream" 的請求體數(shù)據(jù)。
body-parser 并不支持處理 Content-Type 是 "multipart/form-data" 的請求體數(shù)據(jù)。如果你有這方面的需求,可以參照下面的庫進行選擇。
express 從 v4.17.0 開始[10],已全面內(nèi)置了 body-parser 功能,你直接可以通過 express().json() / express().raw()/express().text()/ express().urlencoded() 4 個 API。
在內(nèi)部,這 4 方法其實是 body-parser 借著 express 暴露出來[11]的。也就是說項目中你無需安裝 body-parser 依賴了。
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
本文介紹了 Express 中用于解析請求體數(shù)據(jù)的中間件 body-parser。 其實 Express 在 v4.17.0 中已完整內(nèi)置了 body-parser 的能力,你可以通過 express() 上的 .json()/.urlencoded()/.text()/.raw() 方法訪問到。
就介紹到這里了,希望本文對你有所幫助,感謝閱讀,再見。
[1]type-is: https://www.npmjs.org/package/type-is#readme
[2]bytes: https://www.npmjs.com/package/bytes
[3]MDN 上 JSON.parse() 的使用文檔: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter
[4]busboy: https://www.npmjs.org/package/busboy#readme
[5]connect-busboy: https://www.npmjs.org/package/connect-busboy#readme
[6]multiparty: https://www.npmjs.org/package/multiparty#readme
[7]connect-multiparty: https://www.npmjs.org/package/connect-multiparty#readme
[8]formidable: https://www.npmjs.org/package/formidable#readme
[9]multer: https://www.npmjs.org/package/multer#readme
[10]從 v4.17.0 開始: https://expressjs.com/en/4x/api.html#express.json
[11]body-parser 借著 express 暴露出來: https://github.com/expressjs/express/blob/4.18.2/lib/express.js#L78
本文鏈接: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