神奇的是,使用 https 模块时可以正常访问 uri 并保存在本地,但是使用 request 模块时请求都发不出去,百思不得其解,报错信息如下:
Error: tunneling socket could not be established, cause=getaddrinfo ENOTFOUND 7890
at ClientRequest.onError (D:\www\BWK_Spider\node_modules\tunnel-agent\index.js:177:17)
at Object.onceWrapper (node:events:642:26)
at ClientRequest.emit (node:events:527:28)
at Socket.socketErrorListener (node:_http_client:454:9)
at Socket.emit (node:events:527:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
code: 'ECONNRESET'
}
request 模块代码:
const request = require("request");
const fs = require("fs");
request("https://xxx.xx.xx/20_C12100030.pdf")
.pipe(fs.createWriteStream("C:\\Users\\444\\Desktop\\20_C12100030.pdf"))
.on("close", function (err) {
console.log("下载完成");
console.log(err);
})
.on("error", function (err) {
console.log(err);
});
https 模块代码
const https = require("https");
const file = fs.createWriteStream(
"C:\\Users\\444\\Desktop\\20_C12100030.pdf"
);
https
.get("https://xxx.xx.xx/20_C12100030.pdf", (response) => {
console.log("请求成功");
response.pipe(file);
// after download completed close filestream
file.on("finish", () => {
file.close();
console.log("Download Completed");
});
})
.on("error", (err) => {
console.log("Error: ", err.message);
});
有遇到这种情况的吗?网上搜索都说是 npm 代理导致的,但实际不是,否则使用 https 模块时也应该是请求发不出去才对。
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.