大佬教程收集整理的这篇文章主要介绍了在node.js中使用zlib时检查不正确,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
里面的“zlib.gunzip”方法。我是node.js的新手,感谢任何帮助。
;
firerequest: function() { var rBody = ''; var resBody = ''; var contentLength; var options = { 'encoding' : 'utf-8' }; rBody = fSystem.readFileSync('resources/im.json',options); console.log('Loaded data from im.json ' + rBody); contentLength = Buffer.byteLength(rBody,'utf-8'); console.log('Byte length of the request body ' + contentLength); var httpOptions = { hostname : 'abc.com',path : '/path',method : 'POST',headers : { 'Authorization' : 'Basic VHJhZasfasNWEWFScsdfsNCdXllcje6dHJhZGVjYXJk','Content-Type' : 'application/json; charset=UTF=8',// 'Accept' : '*/*','Accept-Encoding' : 'gzip,deflate,sdch','Content-Length' : contentLength } }; var postrequest = http.request(httpOptions,function(responsE) { var chunks = ''; console.log('Response received'); console.log('STATUS: ' + response.statusCodE); console.log('HEADERS: ' + JSON.Stringify(response.headers)); // response.setEncoding('utf8'); response.setEncoding(null); response.on('data',function(res) { chunks += res; }); response.on('end',function() { var encoding = response.headers['content-encoding']; if (encoding == 'gzip') { zlib.gunzip(chunks,function(err,decoded) { if (err) throw err; console.log('Decoded data: ' + decoded); }); } }); }); postrequest.on('error',function(E) { console.log('Error occured' + E); }); postrequest.write(rBody); postrequest.end(); }
1)收集数组中的所有缓冲区,最终事件将使用Buffer.concat()进行连接。然后调用gunzip的结果。
2)使用.pipe()并将响应传递给gunzip对象,如果要将结果存储在内存中,则将其输出配置为文件流或字符串/缓冲区字符串。
这两个选项(1)和(2)在这里讨论:http://nickfishman.com/post/49533681471/nodejs-http-requests-with-gzip-deflate-compression
以上是大佬教程为你收集整理的在node.js中使用zlib时检查不正确全部内容,希望文章能够帮你解决在node.js中使用zlib时检查不正确所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。