pdog18
V2EX  ›  问与答

咋让本地的 node 正常访问网络?

  •  
  •   pdog18 · Oct 16, 2021 · 1359 views
    This topic created in 1682 days ago, the information mentioned may be changed or developed.

    我有一个脚本需要请求外网 (请求网络通过 'node-fetch' 的 api )

    我的 terminal 是可以访问的,但是通过 node xx.js 运行脚本的时候好像无法访问。是需要对 node 进行某些设置吗?

    4 replies    2021-10-16 15:10:38 +08:00
    JKeita
        1
    JKeita  
       Oct 16, 2021   ❤️ 1
    node 不清楚,反正 go http 相关包可以设置代理。
    pdog18
        2
    pdog18  
    OP
       Oct 16, 2021
    我不太清楚这个 js 脚本里面的 node fetch API 从哪进行联网的,咋才可以把它走的那条网络让代理“覆盖”到,就像 termial 设置一下 https_proxy 之类的
    makelove
        3
    makelove  
       Oct 16, 2021   ❤️ 1
    import HttpsProxyAgent from 'https-proxy-agent'
    const agent = proxy ? new HttpsProxyAgent(proxy) : undefined
    const res = await fetch(url, { agent })
    pdog18
        4
    pdog18  
    OP
       Oct 16, 2021
    @makelove 感谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5854 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 72ms · UTC 03:36 · PVG 11:36 · LAX 20:36 · JFK 23:36
    ♥ Do have faith in what you're doing.