nodejs學(xué)習(xí)筆記——body-parser實(shí)現(xiàn)解析

前言

body-parser是非常常用的一個(gè)express中間件,作用是對(duì)http請(qǐng)求體進(jìn)行解析。
(如果對(duì)http請(qǐng)求不熟悉可以參考個(gè)人事先整理的關(guān)于http事務(wù)剖析


body-parser主要做了什么

// bodyParser.json([options])
// bodyParser.raw([options])
// bodyParser.text([options])
// bodyParser.urlencoded([options])
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

官方提供了這樣幾種api應(yīng)用范例,通過幾種方法名可以看出都是針對(duì)改類型的請(qǐng)求體的。至于options中,可以應(yīng)對(duì)不同的編碼格式、壓縮類型等等。
因此可以看出body-parser完成了以下幾點(diǎn)功能:

  • 處理不同類型的請(qǐng)求體:text、json、urlencoded、raw,對(duì)應(yīng)的報(bào)文主體的格式不同。
  • 處理不同的編碼:比如utf8、gbk等。
  • 處理不同的壓縮類型:比如gzip、deflare等。
  • 其他邊界、異常的處理。

解析不同類型請(qǐng)求體

源代碼非常清晰,根據(jù)4個(gè)類型在lib/type文件夾下單獨(dú)存放方法。

解析text/plain

(raw的解析方式基本一致,只是針對(duì)options做了不同的代碼處理)

// 客戶端代碼
var http = require('http');
var options = {
    hostname: '127.0.0.1',
    port: '3000',
    path: '/test',
    method: 'POST',
    headers: {
        'Content-Type': 'text/plain',
        'Content-Encoding': 'identity'
    }
};
var client = http.request(options, (res) => {
    res.pipe(process.stdout);
});
client.end('hello');
// 服務(wù)端代碼
var http = require('http');
var parsePostBody = function (req, done) {
    var arr = [];
    var chunks;
    req.on('data', buff => {
        arr.push(buff);
    });
    req.on('end', () => {
        chunks = Buffer.concat(arr);
        done(chunks);
    });
};
var server = http.createServer(function (req, res) {
    parsePostBody(req, (chunks) => {
        var body = chunks.toString();
        res.end(`You said ${body}`)
    });
});
server.listen(3000);
解析application/json
// 客戶端代碼
// 更換了Content-Type
var http = require('http');
var options = {
    // 配置options
    headers: {
        'Content-Type': 'application/json',
        'Content-Encoding': 'identity'
    }
};
var client = http.request(options, (res) => {
    res.pipe(process.stdout);
});
client.end( JSON.stringify( say : 'hello' }) );
// 服務(wù)端代碼
var http = require('http');
var parsePostBody = function (req, done) {
    // 同上
};
var server = http.createServer(function (req, res) {
    parsePostBody(req, (chunks) => {
        var json = JSON.parse( chunks.toString() ); // 先用JSON.parse解析
        res.end(`You said ${json.say}`)
    });
});
server.listen(3000);
解析application/x-www-form-urlencoded
// 客戶端代碼
var http = require('http');
var qs = require('qs');
var options = {
    // 配置options
    headers: {
        'Content-Type': 'form/x-www-form-urlencoded'
    }
};
var client = http.request(options, (res) => {
    res.pipe(process.stdout);
});
client.end( qs.stringify({ say : 'hello' }) );
// 服務(wù)端代碼
var http = require('http');
var qs = require('qs');
var parsePostBody = function (req, done) {
    // 同上
};
var server = http.createServer(function (req, res) {
    parsePostBody(req, (chunks) => {
        var body = qs.parse( chunks.toString() ); // 先借助qs解析
        res.end(`You said ${body.say}`)
    });
});
server.listen(3000);

處理不同編碼

有兩個(gè)要點(diǎn):

  • 編碼聲明:在Content-Type最后加上 ;charset=gbk
  • 請(qǐng)求體編碼:這里借助了iconv-lite,對(duì)請(qǐng)求體進(jìn)行編碼
    iconv.encode('你好', 'gbk')
    注:nodejs本身不支持gbk編碼,所以請(qǐng)求發(fā)送前,需要先進(jìn)行編碼
// 客戶端代碼
var http = require('http');
var iconv = require('iconv-lite');
var encoding = 'gbk'; // 請(qǐng)求編碼
var options = {
    // options設(shè)置
    headers: {
        'Content-Type': 'text/plain; charset=' + encoding,
        'Content-Encoding': 'identity',        
    }
};
var buff = iconv.encode('你好', encoding);
var client = http.request(options, (res) => {
    res.pipe(process.stdout);
});
client.end(buff, encoding);

服務(wù)端代碼如下,這里多了兩個(gè)步驟:編碼判斷、解碼操作。首先通過Content-Type獲取編碼類型gbk,然后通過iconv-lite進(jìn)行反向解碼操作。

// 服務(wù)端代碼
var http = require('http');
var contentType = require('content-type');
var iconv = require('iconv-lite');
var parsePostBody = function (req, done) {
    var obj = contentType.parse(req.headers['content-type']);
    var charset = obj.parameters.charset; // 編碼判斷:這里獲取到的值是 'gbk'
    var arr = [];
    var chunks;
    req.on('data', buff => {
        arr.push(buff);
    });
    req.on('end', () => {
        chunks = Buffer.concat(arr);
        var body = iconv.decode(chunks, charset);  // 解碼操作
        done(body);
    });
};
// 以下和先前一致
var server = http.createServer(function (req, res) {
    parsePostBody(req, (body) => {
        res.end(`You said ${body}`)
    });
});
server.listen(3000);

處理不同壓縮類型

和上述處理編碼的方式類似,借助了zlib模塊。
服務(wù)端先判斷Content-Encoding,例如gzip格式壓縮,然后對(duì)請(qǐng)求提進(jìn)行解壓操作

stream = zlib.createGunzip()
req.pipe(stream)

心得總結(jié)

body-parser的核心實(shí)現(xiàn)并不復(fù)雜,很大篇幅是在處理異常和邊界情況,尤其是一些定制化的options處理。
另外對(duì)于POST請(qǐng)求,有一個(gè)非常常見的Content-Type是multipart/form-data,這個(gè)的處理相對(duì)復(fù)雜些,body-parser好像并不打算對(duì)其進(jìn)行支持。


參考

事先整理的關(guān)于http事務(wù)剖析
expressjs/body-parser倉(cāng)庫(kù)地址
程序猿小卡的nodejs學(xué)習(xí)筆記github

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容