【发布时间】:2021-12-28 23:42:13
【问题描述】:
我需要帮助在 nodejs 中实现文件下载器。
所以我需要从服务器下载超过 25'000 个文件。我正在使用node-fetch,但我并不完全知道如何实现这一点。我尝试使用Promise.allSettled(),但我还需要一种方法来限制对服务器的并发请求数量,否则我会受到速率限制。
这是我目前的代码:
const fetch = require('node-fetch')
async function main () {
const urls = [
'https://www.example.com/foo.png',
'https://www.example.com/bar.gif',
'https://www.example.com/baz.jpg',
... many more (~25k)
]
// how to save each file on the machine with same file name and extension?
// how to limit the amount of concurrent requests to the server?
const files = await Promise.allSettled(
urls.map((url) => fetch(url))
)
}
main()
所以我的问题是:
- 如何限制对服务器的并发请求数量?可以使用带有
node-fetch的自定义https 代理并将maxSockets设置为10 来解决这个问题吗? - 如何检查该文件是否存在于服务器上,如果存在则以相同的文件名和扩展名将其下载到我的计算机上?
如果有人可以展示一个小示例代码,我将如何实现此类功能,那将非常有帮助。
提前致谢。
【问题讨论】:
标签: node.js download fetch node-fetch