Download large file with node.js avoiding high memory consumption

后端 未结 6 929
梦毁少年i
梦毁少年i 2020-12-12 15:35

I`m trying to create a file downloader as a background service but when a large file is scheduled, it\'s first put in memory and then, at the end of the download the file is

6条回答
  •  独厮守ぢ
    2020-12-12 15:57

    Take a look at http-request:

    // shorthand syntax, buffered response
    http.get('http://localhost/get', function (err, res) {
        if (err) throw err;
        console.log(res.code, res.headers, res.buffer.toString());
    });
    
    // save the response to 'myfile.bin' with a progress callback
    http.get({
        url: 'http://localhost/get',
        progress: function (current, total) {
            console.log('downloaded %d bytes from %d', current, total);
        }
    }, 'myfile.bin', function (err, res) {
        if (err) throw err;
        console.log(res.code, res.headers, res.file);
    });
    

提交回复
热议问题