fescover
V2EX  ›  Node.js

(node:7804) UnhandledPromiseRejectionWarning: Error: connect ETIMEDOUT

  •  
  •   fescover · Jun 1, 2020 · 4535 views
    This topic created in 2172 days ago, the information mentioned may be changed or developed.

    遇到这么个情况,请大神指教 这个接口 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 replies    2020-06-02 20:33:29 +08:00
    xqin
        1
    xqin  
       Jun 1, 2020
    你发的那个 URL 应该是需要 FQ 的, 你的浏览器估计有开启代理, 所以你浏览器可以打开.

    但你用 axios 请求时没有设置代理, 所以请求不到.
    fescover
        2
    fescover  
    OP
       Jun 1, 2020
    @ddsfeng 我梯子开的全局,axios 走的不是梯子的网吗?
    xqin
        3
    xqin  
       Jun 1, 2020
    @fescover 如果走的话, 你的 node 就请求成功了.
    crystom
        4
    crystom  
       Jun 2, 2020
    @fescover #2 全局没用,要设置环境变量才行的
    ochatokori
        5
    ochatokori  
       Jun 2, 2020 via Android
    你浏览器写的是 https,node 里面写的是 http
    BlackHole1
        6
    BlackHole1  
    PRO
       Jun 2, 2020
    试了下在我这没有问题,考虑在后面添加歌 catch,看下具体的错误原因吧。感觉是网络问题导致的。虽然你写的 http,但是会自动跳转到 https,所以应该和这个没关系。还不行的话,列举下你的环境信息(node/axios veriosn)
    JayLin1011
        7
    JayLin1011  
       Jun 2, 2020
    `Promise` 的缺点之一就是内部跑出异常需要外部主动捕获。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5775 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 50ms · UTC 06:29 · PVG 14:29 · LAX 23:29 · JFK 02:29
    ♥ Do have faith in what you're doing.