使用Axios发送GET请求时遇AxiosError: read ECONNRESET错误求解决
解决Axios发送GET请求时的read ECONNRESET错误
问题场景
使用Axios发送GET请求到https://bored-api.appbrewery.com/random时出现read ECONNRESET错误,相关代码及错误信息如下:
项目配置代码
import express from "express"; import bodyParser from "body-parser"; import axios from "axios"; const app = express(); const port = 3000; app.use(express.static("public")); app.use(bodyParser.urlencoded({ extended: true }));
GET请求代码
app.get("/", async (req, res) => { try { const response = await axios.get("https://bored-api.appbrewery.com/random"); } catch (error) { console.error("Failed to make request:", error); } });
错误信息
Failed to make request: AxiosError: read ECONNRESET at AxiosError.from (file:///C:/Users/BENLOW/Desktop/Web%20Development/2023/28.3%20Axios/node_modules/axios/lib/core/AxiosError.js:89:14) at RedirectableRequest.handleRequestError (file:///C:/Users/BENLOW/Desktop/Web%20Development/2023/28.3%20Axios/node_modules/axios/lib/adapters/http.js:591:25) at RedirectableRequest.emit (node:events:513:28) at eventHandlers.<computed> (C:\Users\BENLOW\Desktop\Web Development\2023\28.3 Axios\node_modules\follow-redirects\index.js:14:24) at ClientRequest.emit (node:events:513:28) at TLSSocket.socketErrorListener (node:_http_client:502:9) at TLSSocket.emit (node:events:513:28) at emitErrorNT (node:internal/streams/destroy:151:8) at emitErrorCloseNT (node:internal/streams/destroy:116:3) at process.processTicksAndRejections (node:internal/process/task_queues:82:21) { syscall: 'read', code: 'ECONNRESET', errno: -4077, ...
ECONNRESET本质是连接被目标服务器或中间网络节点强制重置,多由网络波动、超时配置不合理、服务器负载高等原因导致,以下是几种实用的解决方法:
解决方法
1. 增加请求超时配置
Axios默认无超时限制,网络延迟过高时易触发连接重置,手动设置合理的超时时间(比如10秒):
app.get("/", async (req, res) => { try { const response = await axios.get("https://bored-api.appbrewery.com/random", { timeout: 10000 // 10秒超时 }); res.send(response.data); // 务必给客户端返回响应,避免请求挂起 } catch (error) { console.error("Failed to make request:", error); res.status(500).send("请求失败"); } });
2. 添加请求重试机制
临时网络波动是常见诱因,给请求添加重试逻辑可规避这类问题,推荐用axios-retry插件:
首先安装插件:
npm install axios-retry
然后修改代码:
import axiosRetry from 'axios-retry'; // 配置重试规则:最多重试3次,仅针对ECONNRESET错误 axiosRetry(axios, { retries: 3, retryCondition: (error) => error.code === 'ECONNRESET' }); app.get("/", async (req, res) => { try { const response = await axios.get("https://bored-api.appbrewery.com/random", { timeout: 10000 }); res.send(response.data); } catch (error) { console.error("Failed to make request:", error); res.status(500).send("请求失败"); } });
3. 排查本地网络与代理
- 切换网络(比如WiFi换手机热点),排除本地网络故障;
- 关闭VPN、代理工具,这类工具可能中断HTTPS连接;
- 用浏览器直接访问目标API地址,确认服务器是否正常响应。
4. 更新Axios版本
旧版本Axios可能存在适配器或网络请求的bug,更新到最新版本:
npm update axios
5. 调整HTTP适配器配置
尝试启用连接复用,或禁用异常重定向:
const http = require('http'); const https = require('https'); app.get("/", async (req, res) => { try { const response = await axios.get("https://bored-api.appbrewery.com/random", { timeout: 10000, httpAgent: new http.Agent({ keepAlive: true }), httpsAgent: new https.Agent({ keepAlive: true }), maxRedirects: 0 // 按需禁用重定向 }); res.send(response.data); } catch (error) { console.error("Failed to make request:", error); res.status(500).send("请求失败"); } });
内容的提问来源于stack exchange,提问作者Ben Foo Low
相关产品推荐
相关产品推荐

