使用 node.js (http.get) 读取远程文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14552638/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:01:48  来源:igfitidea点击:

Read remote file with node.js (http.get)

node.js

提问by 7zark7

Whats the best way to read a remote file? I want to get the whole file (not chunks).

读取远程文件的最佳方法是什么?我想获取整个文件(不是块)。

I started with the following example

我从下面的例子开始

var get = http.get(options).on('response', function (response) {
    response.on('data', function (chunk) {
        console.log('BODY: ' + chunk);
    });
});

I want to parse the file as csv, however for this I need the whole file rather than chunked data.

我想将文件解析为 csv,但是为此我需要整个文件而不是分块数据。

回答by 7zark7

I'd use requestfor this:

我会为此使用请求

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'))

Or if you don't need to save to a file first, and you just need to read the CSV into memory, you can do the following:

或者,如果您不需要先保存到文件,而只需要将 CSV 读入内存,则可以执行以下操作:

var request = require('request');
request.get('http://www.whatever.com/my.csv', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var csv = body;
        // Continue with your processing here.
    }
});

etc.

等等。

回答by 7zark7

http.get(options).on('response', function (response) {
    var body = '';
    var i = 0;
    response.on('data', function (chunk) {
        i++;
        body += chunk;
        console.log('BODY Part: ' + i);
    });
    response.on('end', function () {

        console.log(body);
        console.log('Finished');
    });
});

Changes to this, which works. Any comments?

对此进行更改,这很有效。任何意见?

回答by Freddy

You can do something like this, without using any external libraries.

您可以执行此类操作,而无需使用任何外部库。

const fs = require("fs");
const https = require("https");

const file = fs.createWriteStream("data.txt");

https.get("https://www.w3.org/TR/PNG/iso_8859-1.txt", response => {
  var stream = response.pipe(file);

  stream.on("finish", function() {
    console.log("done");
  });
});

回答by Sarath Kumar Rajendran

function(url,callback){
    request(url).on('data',(data) => {
        try{
            var json = JSON.parse(data);    
        }
        catch(error){
            callback("");
        }
        callback(json);
    })
}

You can also use this. This is to async flow. The error comes when the response is not a JSON. Also in 404 status code .

你也可以使用这个。这是异步流程。当响应不是 JSON 时会出现错误。同样在 404 状态代码中。