节点JS不正确地从github url下载文件

我想从github下载文件。 我使用这个问题,它的答案作为例子

我写这个代码:

var downloadPath = './downloads/picasso.zip'; var file = fs.createWriteStream(downloadPath); https.get('https://github.com/square/picasso/archive/master.zip', function (response) { response.pipe(file); file.on('finish', function() { file.close(); console.log("DONE LOADING"); }) }).on('error', function (err) { console.log("ERROR " + err.message); fs.unlink(downloadPath); }); 

当我启动这个代码时,它对我说“ DONE LOADING ,这在理论上意味着zip文件被下载。 但是,当我尝试打开这个zip文件时,我的存档器说这个文件已经损坏,只有1KB。

为什么发生这种情况? github是否会根据我的请求的标题限制下载? 那么为什么错误不是抛出?

wget文件

你可以看到HTTP状态码302,意味着它redirect到其他地址codeload.github.com/square/picasso/zip/master

所以你应该把代码中的链接replace成上面的正确地址,然后你会得到正确的文件。

正确的文件

现在这个文件是1.2M