Answer the question
In order to leave comments, you need to log in
Node.js(Sails, fs, request, bluebird), one of the two files cannot be downloaded.?
Good day to all!
Faced the following problem, there is a script that downloads two files from a remote server. One is bound to fail. When one of the files is downloaded, the second one immediately stops downloading.
The code:
var request = require('request');
var fs = require('fs');
var path = require('path');
var Promise = require('bluebird');
var uploadPath = './uploads/';
var serverUrl = 'http://somewhere.net/';
downloadFile: function(name) {
var self = this;
return new Promise(function(resolve, reject) {
var p = path.join(__dirname, uploadPath + name);
console.log('work with path: ' + p);
var writer = fs.createWriteStream(p);
var time_start = Date.now();
console.log('start downloading', name, 'from: ', serverUrl + name);
var download = request({
url: serverUrl + name,
method: 'get',
timeout: 1000 * 60 * 10
}, function(error, response) {
if (error) {
console.log('request error', error, 'for', (Date.now() - time_start) / 1000, 'seconds');
reject({
reason: 'download failure'
});
}
}).pipe(writer);
Promise.all([
self.downloadFile('FIRST_FILE'),
self.downloadFile('SECOND_FILE')
]).then(SOME_CODE);
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question