Node.js调用外部API出现ConnectTimeoutError问题排查
Node.js v19.6.0调用Alchemy Webhook注册API时出现连接超时(UND_ERR_CONNECT_TIMEOUT)
问题描述
我基于Node.js v19.6.0编写了index.js,通过POST请求调用外部API注册Webhook,已确认待注册的Webhook地址可用且测试通过,但调用/register/hook接口时触发连接超时错误,无法确定问题出在API提供商还是REST请求实现上。
代码与报错信息
实现代码
const express = require('express'); const app = express(); const port = 8080; app.listen(port, () => { console.log(`listening on port ${port}`) }) app.post("/register/hook", (req, res) => { const options = { method: 'POST', headers: { accept: 'application/json', 'X-Alchemy-Token': 'abc...def', 'content-type': 'application/json' }, body: JSON.stringify({ AddressWebhookParams: {addresses: ['0xe592427a0aece92de3edee1f18e0157c05861564']}, url: 'https://webhook.site/dfb04cab-8ca9-40f1-a522-66918d4a7015', type: 'ADDRESS_ACTIVITY' }) }; fetch('https://alchemy-sdk-core-example.com/create-webhook', options) .then(response => response.json()) .then(response => console.log(response)) .catch(err => console.error(err)); })
报错信息
TypeError: fetch failed at Object.fetch (node:internal/deps/undici/undici:12789:11) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) { cause: ConnectTimeoutError: Connect Timeout Error at onConnectTimeout (node:internal/deps/undici/undici:8236:28) at node:internal/deps/undici/undici:8194:50 at Immediate._onImmediate (node:internal/deps/undici/undici:8225:13) at process.processImmediate (node:internal/timers:475:21) { code: 'UND_ERR_CONNECT_TIMEOUT' } }
排查与解决步骤
1. 验证API端点的可达性
直接用curl或Postman请求目标API地址,带上完全相同的请求头和参数,确认是否能正常连接:
curl -X POST https://alchemy-sdk-core-example.com/create-webhook \ -H "accept: application/json" \ -H "X-Alchemy-Token: abc...def" \ -H "content-type: application/json" \ -d '{ "AddressWebhookParams": {"addresses": ["0xe592427a0aece92de3edee1f18e0157c05861564"]}, "url": "https://webhook.site/dfb04cab-8ca9-40f1-a522-66918d4a7015", "type": "ADDRESS_ACTIVITY" }'
- 如果
curl也超时:大概率是API服务商的服务故障,或是你的网络环境(防火墙、代理)阻止了出站请求。 - 如果
curl成功:问题出在Node.js的请求实现上。
2. 核对API端点是否正确
你当前使用的https://alchemy-sdk-core-example.com/create-webhook看起来是示例地址,并非Alchemy的实际可用API端点。请查阅Alchemy官方文档,确认Webhook注册的真实API地址(例如格式可能为https://api.alchemy.com/v2/<你的API密钥>/webhooks)。
3. 调整Node.js fetch的超时配置
Node.js v19的fetch基于undici,默认连接超时较短,可手动延长超时时间测试:
app.post("/register/hook", (req, res) => { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), 10000); // 设置10秒超时 const options = { method: 'POST', headers: { accept: 'application/json', 'X-Alchemy-Token': 'abc...def', 'content-type': 'application/json' }, body: JSON.stringify({ AddressWebhookParams: {addresses: ['0xe592427a0aece92de3edee1f18e0157c05861564']}, url: 'https://webhook.site/dfb04cab-8ca9-40f1-a522-66918d4a7015', type: 'ADDRESS_ACTIVITY' }), signal: controller.signal }; fetch('https://correct-alchemy-api-url/create-webhook', options) .then(response => response.json()) .then(response => { clearTimeout(timeoutId); console.log(response); }) .catch(err => { clearTimeout(timeoutId); console.error(err); }); })
4. 替换HTTP库测试
如果调整超时后仍失败,可尝试用axios等第三方HTTP库替代原生fetch,排查是否是undici的兼容性问题:
const express = require('express'); const axios = require('axios'); const app = express(); const port = 8080; app.listen(port, () => { console.log(`listening on port ${port}`) }) app.post("/register/hook", (req, res) => { axios.post('https://correct-alchemy-api-url/create-webhook', { AddressWebhookParams: {addresses: ['0xe592427a0aece92de3edee1f18e0157c05861564']}, url: 'https://webhook.site/dfb04cab-8ca9-40f1-a522-66918d4a7015', type: 'ADDRESS_ACTIVITY' }, { headers: { accept: 'application/json', 'X-Alchemy-Token': 'abc...def', 'content-type': 'application/json' }, timeout: 10000 }) .then(response => console.log(response.data)) .catch(err => console.error(err)); })
5. 检查网络环境与服务状态
- 确认你的服务器/本地环境没有防火墙、代理或VPN阻止访问Alchemy的API域名;
- 查看Alchemy官方服务状态页面,确认是否有服务中断或维护通知。
内容的提问来源于stack exchange,提问作者WowBow
相关产品推荐
相关产品推荐

