clem
clem

Reputation: 35

node stream error

Hi i'm currently playing with node and got an error i dont know how to fix :

stream.js:81
throw er; // Unhandled stream error in pipe.

Error: socket hang up
    at createHangUpError (http.js:1344:15)
    at Socket.socketOnEnd [as onend] (http.js:1432:23)
    at TCP.onread (net.js:419:26)

My script read a file which contains urls of a pages who themselves contains url of an image i download and save. Can you explain how to fix this ? Sorry for my english...

var fs = require('fs');
var request = require('request');
var lazy = require('lazy');

var lines = new lazy(fs.createReadStream('./' + dir + '/links.txt'))
    .lines
    .forEach(function (line) {

        var req = request(line, function (error, res, body) {

            if(res.statusCode == 200) {
                var urlImg = body.match(/<img id="img".+?src="(.+?)"/)[1];
                var stream = fs.createWriteStream(urlImg);
                var req2 = request(urlImg).pipe(stream);
            };
        });

    });

Upvotes: 0

Views: 1406

Answers (1)

Benoir
Benoir

Reputation: 1244

It could be that your making too many simultaneous requests.

request(..) returns immediately, so you have the potential to execute a ton of http requests. Try sending them in smaller batches (i.e., send 100, then wait for all to return, send another 100), etc.

Upvotes: 1

Related Questions