node-http-proxy修改響應(yīng)結(jié)果實(shí)例代碼
最近在項(xiàng)目中使用node-http-proxy遇到需要修改代理服務(wù)器響應(yīng)結(jié)果需求,該庫已提供修改響應(yīng)格式為html的方案:Harmon,而項(xiàng)目中返回格式統(tǒng)一為json,使用它感覺太笨重了,所以自己寫了個(gè)可解析和修改json格式的庫,
期間也遇到了之前未關(guān)注的問題:http傳輸編碼、node流的相關(guān)處理。下面是實(shí)現(xiàn)代碼:
var zlib = require('zlib'); var concatStream = require('concat-stream'); /** * Modify the response of json * @param res {Response} The http response * @param contentEncoding {String} The http header content-encoding: gzip/deflate * @param callback {Function} Custom modified logic */ module.exports = function modifyResponse(res, contentEncoding, callback) { var unzip, zip; // Now only deal with the gzip and deflate content-encoding. if (contentEncoding === 'gzip') { unzip = zlib.Gunzip(); zip = zlib.Gzip(); } else if (contentEncoding === 'deflate') { unzip = zlib.Inflate(); zip = zlib.Deflate(); } // The cache response method can be called after the modification. var _write = res.write; var _end = res.end; if (unzip) { unzip.on('error', function (e) { console.log('Unzip error: ', e); _end.call(res); }); } else { console.log('Not supported content-encoding: ' + contentEncoding); return; } // The rewrite response method is replaced by unzip stream. res.write = function (data) { unzip.write(data); }; res.end = function (data) { unzip.end(data); }; // Concat the unzip stream. var concatWrite = concatStream(function (data) { var body; try { body = JSON.parse(data.toString()); } catch (e) { body = data.toString(); console.log('JSON.parse error:', e); } // Custom modified logic if (typeof callback === 'function') { body = callback(body); } // Converts the JSON to buffer. body = new Buffer(JSON.stringify(body)); // Call the response method and recover the content-encoding. zip.on('data', function (chunk) { _write.call(res, chunk); }); zip.on('end', function () { _end.call(res); }); zip.write(body); zip.end(); }); unzip.pipe(concatWrite); };
項(xiàng)目地址:node-http-proxy-json,歡迎大家試用提意見,同時(shí)不要吝嗇Star。
在該庫的實(shí)現(xiàn)過程中越發(fā)覺得理論知識的重要性,所謂理論是行動(dòng)的先導(dǎo),之前都是使用第三方庫,也沒去關(guān)心一些底層的細(xì)節(jié)處理。
后面有空一定要多看看底層的實(shí)現(xiàn),否則遇到難搞問題就卡住了。
以上所述是小編給大家介紹的node-http-proxy修改響應(yīng)結(jié)果實(shí)例代碼,希望對大家有所幫助!
- Node.js配合node-http-proxy解決本地開發(fā)ajax跨域問題
- node跨域轉(zhuǎn)發(fā) express+http-proxy-middleware的使用
- 拋棄Nginx使用nodejs做反向代理服務(wù)器
- 教你如何使用node.js制作代理服務(wù)器
- 詳解node.js搭建代理服務(wù)器請求數(shù)據(jù)
- node實(shí)現(xiàn)簡單的反向代理服務(wù)器
- 騰訊云(ubuntu)下安裝 nodejs + 實(shí)現(xiàn) Nginx 反向代理服務(wù)器
- 8 行 Node.js 代碼實(shí)現(xiàn)代理服務(wù)器
- node.js使用 http-proxy 創(chuàng)建代理服務(wù)器操作示例
相關(guān)文章
JS鼠標(biāo)滑過圖片時(shí)切換圖片實(shí)現(xiàn)思路
在瀏覽網(wǎng)頁時(shí)會看到這樣的效果:當(dāng)鼠標(biāo)滑過一張圖片后,這張圖片切換為了另外的一張圖片,下面為大家介紹下具體是如何實(shí)現(xiàn)的,感興趣的朋友不要錯(cuò)過2013-09-09動(dòng)態(tài)創(chuàng)建script在IE中緩存js文件時(shí)導(dǎo)致編碼的解決方法
這篇文章主要介紹了動(dòng)態(tài)創(chuàng)建script在IE中緩存js文件時(shí)導(dǎo)致編碼的解決方法,需要的朋友可以參考下2014-05-05文字溢出實(shí)現(xiàn)溢出的部分再放入一個(gè)新生成的div中具體代碼
說文字溢出,如何實(shí)現(xiàn)溢出的文字放入一個(gè)新生成的div中,原理就是判斷是否能在div里放下,如果不能,則在應(yīng)該斷開的地方,差入到新的div中2013-05-05js 創(chuàng)建對象 經(jīng)典模式全面了解
下面小編就為大家?guī)硪黄猨s 創(chuàng)建對象 經(jīng)典模式全面了解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08解決 viewer.js 動(dòng)態(tài)更新圖片導(dǎo)致無法預(yù)覽的問題
Viewer.js 是一款強(qiáng)大的圖片查看器,這篇文章主要介紹了解決 viewer.js 動(dòng)態(tài)更新圖片導(dǎo)致無法預(yù)覽的問題 ,需要的朋友可以參考下2019-05-05fixedBox固定div漂浮代碼支持ie6以上大部分主流瀏覽器
本例為大家分享的是fixedBox固定div漂浮代碼支持ie6以上大部分瀏覽器,需要的朋友可以參考下2014-06-06原生JavaScript實(shí)現(xiàn)模態(tài)框的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用原生JavaScript封裝實(shí)現(xiàn)模態(tài)框效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06JS button按鈕實(shí)現(xiàn)submit按鈕提交效果
今天在使用表單是同時(shí)使用POST更新、刪除操作。然而form表單的 submit 且一旦提交則全部提交,所以想到的實(shí)現(xiàn)方法就是 使用button實(shí)現(xiàn),怎么實(shí)現(xiàn)呢?下面小編給大家分享JS button按鈕實(shí)現(xiàn)submit按鈕提交效果,感興趣的朋友參考下吧2016-11-11