i am quite new to the node.js world, but i wanted to give it a try.
I am trying to set up an Server with Express which exposes an public API.
my code is pretty much set up, but i got an problem where i don't know why it is happening.
this is what it does:
- check if the passed URL is online.
- if the target is an Image, download it locally
- after the download finished, make a thumbnail from the source
.
function requestPromise(url, path) {
return new Promise(function(fulfill, reject) {
request(url, function(err, httpResponse, body) {
if(err) {
reject(err);
} else {
fulfill(path);
}
}).pipe(
fs.createWriteStream(path)
);
});
}
function imageResizeToThumbnail(path) {
return new Promise(function(fulfill, reject) {
gm(path)
.resize(300, 300, ">")
.write(path, function(err) {
if (err) return reject(err);
fulfill(path);
});
});
}
function fetchImage(url) {
return new Promise(function(fulfill, reject) {
// creates a filesystem friendly string
var path = './download/' + sanitize(url, {replacement: '_'});
requestPromise(url, path).then(function(path) {
return imageResizeToThumbnail(path);
}).then(function(path) {
fulfill(path);
}).catch(function(err) {
reject(err);
});
});
}
following test works perfectly, the image gets downloaded and after it is downloaded it gets shrunk down
var test1 = 'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png';
fetchImage(test1).then(function(response) {
console.log(response, ' worked!');
});
then i've tried with a couple images at once, no problem at all
var test2 = [
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png',
'https://s.ytimg.com/yts/img/favicon_144-vflWmzoXw.png'
]
Promise.all(test2.map(function(item) {
return fetchImage(item).then(function(response) {
console.log(response, ' worked!');
});
})).then(function(result) {
console.log('all Promises done!');
}).catch(function(err) {
console.log('error: ', err)
});
then i took the same example as above and run it with 1K items!
the result was:
imageResizeToThumbnail
was getting an error, because the image wasn't downloaded yet...
the file was created, but not finished (filesize was 0bytes)
so my question is... is there a limitation of how many Filestreams can be instantiated or piped to fs?