javascript - 获取“在 ClientRequest 处挂断套接字。" 使用 node-fetch 调用 GET 请求时
问题描述
我正在使用 node-fetch 模块和 javascript 来处理 API 请求。下面是我的代码(只是一个示例 GET 操作)
var fetch = require('node-fetch');
const HttpsProxyAgent = require('https-proxy-agent');
fetch('http://postalpincode.in/api/pincode/600042', {method: 'GET', agent: new HttpsProxyAgent('http://10.10.104.4:50683') })
.then(resRaw=>{
resRaw.toJson();
})
.then(resJson=>{
console.log(resJson);
})
但我收到以下错误。请帮我解决这个问题。我已经安装了 node-fetch、https-proxy-agent 模块。
FetchError: request to http://postalpincode.in/api/pincode/600042 failed, reason: socket hang up
at ClientRequest. (F:\johny\three projects\test-nodefetch\node_modules\node-fetch\lib\index.js:1345:11)
at emitOne (events.js:116:13)
at ClientRequest.emit (events.js:211:7)
at Socket.socketOnEnd (_http_client.js:423:9)
at emitNone (events.js:111:20)
at Socket.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)
(node:19424) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19424) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
解决方案
推荐阅读
- macos - karabiner 元素(和 xmodmap):使用左命令作为 ctrl 和右命令作为 alt 不适用于键盘上的 zxcv 行
- python - 为什么相同正则表达式的 python re.findall() 会为两个等效的输入字符串产生不同的结果?
- c - C中原子变量的比较
- twig - 如何将动态值传递给 twig/craft cms 中的另一个组件?
- bash - 为什么函数定义中的花括号前需要空格?
- android - 我可以创建一个歌曲数组并使用 shuffle() 方法吗?
- css - 需要复杂的(我认为)第 n 个子选择器
- c# - 如何从字典列表中删除特定元素
- c++ - 用于粒子系统/优化的 OpenGL 预计算顶点/矩阵
- rust - 为什么我不必为类型实现 Any 特征,即使它是必需的?