如何使用Node.js without using third-party libraries下载文件?
我不需要什么特别的东西.我只想从给定的URL下载一个文件,然后将其保存到给定的目录.
如何使用Node.js without using third-party libraries下载文件?
我不需要什么特别的东西.我只想从给定的URL下载一个文件,然后将其保存到给定的目录.
您可以创建HTTP GET
请求,并将其response
导入可写文件流:
const http = require('http'); // or 'https' for https:// URLs
const fs = require('fs');
const file = fs.createWriteStream("file.jpg");
const request = http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(response) {
response.pipe(file);
});
如果您想支持在命令行上收集信息——比如指定目标文件或目录,或URL——请查看Commander之类的内容.