Node.js HTTP GET不返回所有数据

时间:2011-08-14 19:46:07

标签: http curl node.js

Node的HTTP模块未返回此URL中的所有预期数据:http://itradedata.co.za/pmchart/prices/GetPricesTRI.asp?Shortname=ACE&numprices=5

在浏览器中查看时有5个价格记录,但在Node的HTTP GET数据中只有一个。数据包嗅探显示所有数据进入,所以为什么它不在输出中?

var http = require('http'),
    host = 'itradedata.co.za',
    records = 5,
    url = '/pmchart/prices/GetPricesTRI.asp?Shortname=ACE&numprices=' + records,
    client = http.createClient(80, host),
    headers = { 'host': host },
    req;

req = client.request('GET', url, headers);
req.on('response', function(res) {
    console.log(res.statusCode);
    res.on('data', function (chunk) {
        console.log(chunk.toString());
    });
});
req.end();

问题似乎是服务器返回数据的方式... cURL 在终端中运行curl {url}时也不会显示数据,但在使用时会将所有内容写入文件curl {url} -o {file}。这是怎么回事?

预期结果:

# Attempting to get 5 prices for theTicker: ACE<BR>
# Trying JSECODE<BR>
# Sending prices for Entity_ID: 17352 Shortname:  ShareID:  JSECode: <BR>
2011-8-15,46,46,46,46,0,08268
2011-8-12,46,46,46,46,51,0068
2011-8-11,46,46,46,46,51,0068
2011-8-10,46,46,46,46,51,0068
2011-8-8,46,46,46,46,51,00068

实际结果:

# Attempting to get 5 prices for theTicker: ACE<BR>
# Trying JSECODE<BR>
# Sending prices for Entity_ID: 17352 Shortname:  ShareID:  JSECode: <BR>
2011-8-8,46,46,46,46,51,00068

四个缺失的记录在结果的中间。

1 个答案:

答案 0 :(得分:2)

我相对肯定是因为该网站发送了\r,但没有\n

缓冲内容:

2c 30 30 0d 32 30

0d说“回到线的开头”,0a说“下一行”。所以它总是会回来,但永远不会写出新的一行。它只是覆盖了现有的输出。

您可以尝试用0a替换0d以查看所有内容。

编辑:尝试过,它有效。代码:

req = client.request('GET', url, headers);
req.on('response', function(res) {
    console.log(res.statusCode);
    res.on('data', function (chunk) {
        for (var i=0; i<chunk.length; i++)
            if (chunk[i] === 0xd) chunk[i] = 0xa
        console.log(chunk.toString());
    });
});
req.end();