To download a file using Node.js
you can use a http
request module and use stream
response to generate a file. In my example I'll use the module request
. To install it use the command:
npm i request
The following function will perform the download
of the file from a URL
to the destination folder:
const { createWriteStream } = require('fs');
const request = require('request');
const baixar = (url) => {
return new Promise((resolver) => {
const partes = url.split('/');
const arquivo = createWriteStream(partes[partes.length - 1]);
request(url).on('response', (resposta) => {
const stream = resposta.pipe(arquivo);
stream.on('finish', resolver);
});
});
};
To download multiple files you can create a function that divides a array
into smaller segments to perform in parallel and execute the call within a loop repetition:
const dividir = (origem, tamanho) => {
const segmentos = [];
for (let i = 0; i < origem.length; i += tamanho) {
segmentos.push(origem.slice(i, i + tamanho));
}
return segmentos;
};
(async () => {
const links = [
"http://localhost/arquivos/arquivo1.png",
"http://localhost/arquivos/arquivo2.png",
"http://localhost/arquivos/arquivo3.png",
"http://localhost/arquivos/arquivo4.png",
"http://localhost/arquivos/arquivo5.png",
"http://localhost/arquivos/arquivo6.png",
"http://localhost/arquivos/arquivo7.png",
"http://localhost/arquivos/arquivo8.png",
];
const partes = dividir(links, 3);
for (parte of partes) {
const promessas = parte.map(link => baixar(link));
await Promise.all(promessas);
}
})();