Fs.createReadStream and .pipe ()

I try to send a large file to another server using the request.post()request package in Node.js, and I get:

(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, because it is a large file (80 MB), because small files work fine, so I'm 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 message:

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

Does anyone know the syntax for including parameters in a pipe and why am I getting this error?

UPDATE (after sousa comment): I tried:

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

This is not a mistake, but it also does nothing. The program just executes and I never see console.log()?

UPDATE # 2:

Using the reference guide here , we figured out that you can do this:

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

, request.post() , , ! :

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) ); 
                }
            }
        )
    );

, :

Apache Tomcat/5.5.17 - HTTP 411 - ( ).

+4
2

var . - :

readStream.pipe(request(options))

, on.

+5

# 2: github " . - PUT"

, , Apache Tomcat , Content-Length :

    var stats = fs.statSync('file.json');

:

    Content-Length':stats.size

0

Source: https://habr.com/ru/post/1528107/


All Articles