node.js 当响应被分块时获取整个响应主体?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5083914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the whole response body when the response is chunked?
提问by ajsie
I'm making a HTTP request and listen for "data":
我正在发出 HTTP 请求并侦听“数据”:
response.on("data", function (data) { ... })
The problem is that the response is chunked so the "data" is just a piece of the body sent back.
问题是响应是分块的,所以“数据”只是发回的主体的一部分。
How do I get the whole body sent back?
怎么把全身都送回来?
回答by Pero P.
request.on('response', function (response) {
var body = '';
response.on('data', function (chunk) {
body += chunk;
});
response.on('end', function () {
console.log('BODY: ' + body);
});
});
request.end();
回答by Jim Danz
Over at https://groups.google.com/forum/?fromgroups=#!topic/nodejs/75gfvfg6xuc, Tane Piper provides a good solution very similar to scriptfromscratch's, but for the case of a JSON response:
在https://groups.google.com/forum/?fromgroups=#!topic/nodejs/75gfvfg6xuc 上,Tane Piper 提供了一个与 scriptfromscratch 非常相似的很好的解决方案,但对于 JSON 响应的情况:
request.on('response',function(response){
var data = [];
response.on('data', function(chunk) {
data.push(chunk);
});
response.on('end', function() {
var result = JSON.parse(data.join(''))
return result
});
});`
This addresses the issue that OP brought up in the comments section of scriptfromscratch's answer.
这解决了 OP 在 scriptfromscratch 答案的评论部分中提出的问题。
回答by schaermu
I never worked with the HTTP-Client library, but since it works just like the server API, try something like this:
我从未使用过 HTTP-Client 库,但由于它的工作方式与服务器 API 一样,请尝试以下操作:
var data = '';
response.on('data', function(chunk) {
// append chunk to your data
data += chunk;
});
response.on('end', function() {
// work with your data var
});
See node.js docsfor reference.
请参阅node.js 文档以供参考。
回答by Salar
In order to support the full spectrum of possible HTTP applications, Node.js's HTTP API is very low-level. So data is received chunk by chunk not as whole.
There are two approaches you can take to this problem:
为了支持所有可能的 HTTP 应用程序,Node.js 的 HTTP API 非常低级。因此,数据是逐块接收的,而不是整个接收。
有两种方法可以解决这个问题:
1) Collect data across multiple "data" events and append the results
together prior to printing the output. Use the "end" event to determine
when the stream is finished and you can write the output.
1) 跨多个“数据”事件收集数据,
并在打印输出之前将结果附加在一起。使用“结束”事件来确定
流何时完成,您可以写入输出。
var http = require('http') ;
http.get('some/url' , function (resp) {
var respContent = '' ;
resp.on('data' , function (data) {
respContent += data.toString() ;//data is a buffer instance
}) ;
resp.on('end' , function() {
console.log(respContent) ;
}) ;
}).on('error' , console.error) ;
2) Use a third-party package to abstract the difficulties involved in
collecting an entire stream of data. Two different packages provide a
useful API for solving this problem (there are likely more!): bl (Buffer
List) and concat-stream; take your pick!
2)使用第三方包来抽象
收集整个数据流所涉及的困难。两个不同的包提供了一个
有用的 API 来解决这个问题(可能还有更多!):bl(缓冲区
列表)和 concat-stream;随你挑!
var http = require('http') ;
var bl = require('bl') ;
http.get('some/url', function (response) {
response.pipe(bl(function (err, data) {
if (err) {
return console.error(err)
}
data = data.toString() ;
console.log(data) ;
}))
}).on('error' , console.error) ;
回答by Martin
The reason it's messed up is because you need to call JSON.parse(data.toString()). Data is a buffer so you can't just parse it directly.
它搞砸的原因是因为你需要调用 JSON.parse(data.toString())。数据是一个缓冲区,所以你不能直接解析它。
回答by tim
If you don't mind using the request library
如果您不介意使用请求库
var request = require('request');
request('http://www.google.com', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body) // Print the google web page.
}
})

