(node:7804) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT

2020-06-01 21:21:17 +08:00
 fescover

遇到这么个情况,请大神指教 这个接口 https://api.testnet.huobi.pro/v1/common/symbols 在浏览器上可以正常访问返回数据:

但是使用 axios 请求却会报错:

报错信息: C:\Users\Maxzel\Desktop\huobi>node app.js (node:7804) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT 174.37.154.236:80 at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) (node:7804) 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(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (node:7804) [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.

之后尝试改 headers,将 axios 换成 request,superagent 来请求都无法得到返回数据,但为什么浏览器又可以直接返回呢?应该怎么解决?

4234 次点击
所在节点    Node.js
7 条回复
xqin
2020-06-01 21:25:58 +08:00
你发的那个 URL 应该是需要 FQ 的, 你的浏览器估计有开启代理, 所以你浏览器可以打开.

但你用 axios 请求时没有设置代理, 所以请求不到.
fescover
2020-06-01 21:32:52 +08:00
@ddsfeng 我梯子开的全局,axios 走的不是梯子的网吗?
xqin
2020-06-01 22:24:59 +08:00
@fescover 如果走的话, 你的 node 就请求成功了.
crystom
2020-06-02 01:58:38 +08:00
@fescover #2 全局没用,要设置环境变量才行的
ochatokori
2020-06-02 02:03:16 +08:00
你浏览器写的是 https,node 里面写的是 http
BlackHole1
2020-06-02 09:07:36 +08:00
试了下在我这没有问题,考虑在后面添加歌 catch,看下具体的错误原因吧。感觉是网络问题导致的。虽然你写的 http,但是会自动跳转到 https,所以应该和这个没关系。还不行的话,列举下你的环境信息(node/axios veriosn)
JayLin1011
2020-06-02 20:33:29 +08:00
`Promise` 的缺点之一就是内部跑出异常需要外部主动捕获。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/677622

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX