在文件中写入http流

时间:2017-06-26 16:27:42

标签: javascript node.js file http request

我在尝试将块缓冲区写入文件时遇到问题。我的问题基本上是文件只包含输出缓冲区的最后一个字节。块缓冲区非常大,我的file.out被截断,最后一项是正确的。

这是我的代码

    var reqChart = http.request(chart, function(res1) {

    res1.setEncoding( 'utf8' );

    res1.on('data', function (chunk) {

        var fs = require('fs');


        //var b = new Buffer(chunk.length);
        var c = "";

        for (var i = 0;i < chunk.length;i++) {
         //   b[i] = chunk[i];
            c = c  + chunk[i]
        }

        console.log(c);

        fs.writeFile("rich.txt", c,  "utf-8",function(err) {
            if(err) {
                console.log(err);
            } else {
                console.log("The file was saved!");
            }
        }); 

    });
});

如果你对这里感兴趣的是BEGINNING我在我的文件中输入的输入与我的变量的“console.log”的BEGINNING ...我已经尝试将“console.log”写入文件但是相同的结果。

文件

0,0.120179572670496],[1498028100000,0.105581318680705],[1498028400000,0.167319933562371],[1498028700000,0.147574197583768],[1498029000000,0.114172853959319],[1498029300000,0.241186960587686],[1498029600000,1.45701290672775],[1498029900000,0.154756395075166],[1498030200000,0.0836631006369253],[1498030500000,0.0800486694467188],[1498030800000,0.0861569133026863],[1498031100000,0.092360872083502],[1498031400000,0.0790707908354345],[1498031700000,0.129877438815933],[1498032000000,0.118812121796025],[1498032300000,0.0989746376933164],[1498032600000,2.30001837257628],[1498032900000,0.313639093279438],[1498033200000,0.227936449562983],

缓冲器/变量/日志

{"requestData":{"options":{"width":950,"gui_component_mode":1,"exporttocsv":1,"only_stacks_in_legend":false,"reverse_axis":false,"height":446,"datasetTitle":"","legend_average":false,"legend_maximum":false,"legend_minimum":false},"model_group":101,"name":"autil_17","model":"nmis_graph","dataset_id":0,"owner_cid":"c3","data_source_type":"chart","model_view":"graph","parameters":{"graph_type":"interface","resource_index":"17","nmis_data_type":null,"value_column":"value","node":"RT01459","axis":0,"end_date_raw":1498095300,"substitutions":{"time.start":1497922702,"time.end":1498095502},"time_column":"time","translation":"","field":"","lineType":"line","period":"2d","index_graph_type":"autil","resource":"interface","start_date_raw":1497922500,"resolution":300,"class":null},"data_source":"local_nmis","translation":null},"replyData":{"options":{"subtitleText":"ENTERPRISE_RT01","titleText":"Interface gigabitethernet0-2-3913","legend_raw":"    Avg In     Max In Avg 12.76 % Max 98.99 % \\n     Avg Out     Max Out Avg 4.98 % Max 52.49 % \\n","yAxis0TitleText":"% Avg Util"},"stacking":"normal","meta_data":{"time_start":1497922500,"start_date_input":"2017-06-19 20:35:00","end_date_input":"2017-06-21 20:35:00"},"data":[{"yAxis":0,"reverse_axis":0,"valueDecimals":2,"value_min":-98.9864025844157,"color":"#00BFFF","suffix":"","dataset_multiplier":1,"sum":-7373.17229868631,"connectNulls":0,"stack":1,"value_max":-0.0591203443255937,"name":"Avg In","data":[[1497922500000,-0.7137202476565],[1497922800000,-1.43305756579003],[1497923100000,-0.150464409649807],[1497923400000,-0.150475661479925],[1497923700000,-0.100369773564214],[1497924000000,-0.0893947123021048]

我想也许是在超时功能或其他什么东西,但是日志写的是信息终端,也许我错过了什么。

3 个答案:

答案 0 :(得分:1)

您遇到的问题是您在stream.on('data',...时正在编写文件。您需要将所有数据添加到文件中,并使用stream.on('end',...事件写入文件。我希望这个例子有帮助

var fs = require('fs');
// Some example of getting original data
var readableStream = fs.createReadStream('file.txt');
var data = '';

readableStream.on('data', function(chunk) {
    data+=chunk;
});

readableStream.on('end', function() {
    console.log(data);
    fs.writeFile("rich.txt", data,  "utf-8",function(err) {
        if(err) {
            console.log(err);
        } else {
            console.log("The file was saved!");
        }
    }); 
});

现在复制代码并对其进行修改:

var reqChart = http.request(chart, (res1) => {
    res1.setEncoding('utf8');
    var fs = require('fs');
    var c = "";

    res1.on('data', function (chunk) {
        //var b = new Buffer(chunk.length);
        for (var i = 0; i < chunk.length; i++) {
            //   b[i] = chunk[i];
            c = c + chunk[i]
        }
        console.log(c);
    });

    res1.on('end', function () {
        fs.writeFile("rich.txt", c, "utf-8", function (err) {
            if (err) {
                console.log(err);
            } else {
                console.log("The file was saved!");
            }
        });
    })
});

答案 1 :(得分:1)

流的pipe功能可以让您的生活更轻松。

const fs = require('fs');
const http = require('http');

const output = fs.createWriteStream('output.html'); // change to rich.txt
const url = 'http://example.com'; // change to chart URL

http.get(url, response => {
    response.pipe(output);
});

答案 2 :(得分:1)

您意识到在每个块上重置c作为其本地作用域?:

var c="";//not resetted
res1.on('data', function (chunk) {
    c+=chunk;
}

你需要等待流完成:

res1.on("end",function(){
 console.log(c);
var fs = require('fs');
 fs.writeFile("rich.txt", c,  "utf-8",function(err) {
        if(err) {
            console.log(err);
        } else {
            console.log("The file was saved!");
        }
  }); 
});