R
R
Robot Chappie2016-08-20 21:23:17
JavaScript
Robot Chappie, 2016-08-20 21:23:17

How to disable asynchrony in node.js http.get()?

The point is the following. There is a file with a lot of links to different files. These files need to be downloaded.
I try to do it like this:

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


function readFile(nameFile) {
    fs.readFile(nameFile, 'utf8', function(err, contents) {
        if (!err) {
            readLine(contents.split('\n'));
        } else {
            console.log('Ошибка', err);
        }
    });
}

function readLine(array) {
    array.forEach(function(uri) {
        var img_name = decodeURIComponent(uri.split('/').slice(-1))
        var file = fs.createWriteStream(img_name);
        var request = http.get(uri, function (response) {
            console.log(img_name, ' OK')
            response.pipe(file);
        });
    });
}

readFile('test.txt');

If there are not very many links in the file, then this script copes, but if there are more than 5 thousand links, then the script crashes. I would like to make it synchronous, as soon as the file is downloaded, only then go to the next link. All experiments failed. Please tell me, can there be any way to make requests synchronously.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question