V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
fescover
V2EX  ›  Node.js

(node:7804) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT

  •  
  •   fescover · 2020-06-01 21:21:17 +08:00 · 3863 次点击
    这是一个创建于 1422 天前的主题,其中的信息可能已经有所发展或是发生改变。

    遇到这么个情况,请大神指教 这个接口 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 来请求都无法得到返回数据,但为什么浏览器又可以直接返回呢?应该怎么解决?

    7 条回复    2020-06-02 20:33:29 +08:00
    ddsfeng
        1
    ddsfeng  
       2020-06-01 21:25:58 +08:00
    你发的那个 URL 应该是需要 FQ 的, 你的浏览器估计有开启代理, 所以你浏览器可以打开.

    但你用 axios 请求时没有设置代理, 所以请求不到.
    fescover
        2
    fescover  
    OP
       2020-06-01 21:32:52 +08:00
    @ddsfeng 我梯子开的全局,axios 走的不是梯子的网吗?
    ddsfeng
        3
    ddsfeng  
       2020-06-01 22:24:59 +08:00
    @fescover 如果走的话, 你的 node 就请求成功了.
    crystom
        4
    crystom  
       2020-06-02 01:58:38 +08:00
    @fescover #2 全局没用,要设置环境变量才行的
    ochatokori
        5
    ochatokori  
       2020-06-02 02:03:16 +08:00 via Android
    你浏览器写的是 https,node 里面写的是 http
    BlackHole1
        6
    BlackHole1  
       2020-06-02 09:07:36 +08:00
    试了下在我这没有问题,考虑在后面添加歌 catch,看下具体的错误原因吧。感觉是网络问题导致的。虽然你写的 http,但是会自动跳转到 https,所以应该和这个没关系。还不行的话,列举下你的环境信息(node/axios veriosn)
    JayLin1011
        7
    JayLin1011  
       2020-06-02 20:33:29 +08:00
    `Promise` 的缺点之一就是内部跑出异常需要外部主动捕获。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5750 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 02:47 · PVG 10:47 · LAX 19:47 · JFK 22:47
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.