Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fs.createReadStream and .pipe()

I'm trying to post a large file to another server using request.post() of the request package in Node.js and I'm getting:

(node) warning: Recursive process.nextTick detected. This will break in the next version    of node. Please use setImmediate for recursive deferral.

RangeError: Maximum call stack size exceeded

I thought it's because it's a big file (80MB) because the smaller files work fine, so I am now trying to implement a stream:

var options = {
    url: aURLIHaveDefined,
    headers: {
        'User-Agent': 'MyRestAPI'
    },
    auth: {
        username: creds.username,
        password: creds.password
    },
    strictSSL: false,
    json: true
}

var readStream = fs.createReadStream(filePathIHaveDefined);

readStream.pipe(options);

And I get the error:

TypeError: Object #<Object> has no method 'on'

Does anyone know the syntax to include options in the pipe and why I'm getting this error?

UPDATE (after sousa comment): I tried:

readStream.pipe(request(options));
readStream.on('end', function() {
     console.log('end of readStream'+fileName);
});

It doesn't error out but it also doesn't seem to do anything now. The program just executes and I never see the console.log() ?

UPDATE #2:

Using the help manual here, found that you can do this:

fs.createReadStream('file.json').pipe(request.put('http://....'))

So I added the request.post() inside the pipe and it seems like I'm almost there! Using:

readStream.pipe(
    request.post(options, 
         function(error, response, data) {  

                if (error) {
                    console.log(": Cannot upload file to " + creds.server + " due to " + error);
                    setImmediate(failureCallback(error)); 
                } else {
                    console.log(": file uploaded to " + data);
                    // send back data, which is url of uploaded file
                    setImmediate( successCallback(data) ); 
                }
            }
        )
    );

I get an error saying the length needs to be known:

Apache Tomcat/5.5.17 - Error report HTTP Status 411 - Length Required his request cannot be handled without a defined content length (Length Required).

like image 804
SOUser Avatar asked Feb 21 '14 13:02

SOUser


1 Answers

The options var needs to be a stream. You have to do something like this:

readStream.pipe(request(options))

You're piping to an object, that's why you have no method on.

like image 163
sousa Avatar answered Oct 11 '22 18:10

sousa