V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
pdog18
V2EX  ›  问与答

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

  •  
  •   pdog18 · 2021-10-16 13:42:37 +08:00 · 929 次点击
    这是一个创建于 894 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

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

    4 条回复    2021-10-16 15:10:38 +08:00
    JKeita
        1
    JKeita  
       2021-10-16 14:00:37 +08:00   ❤️ 1
    node 不清楚,反正 go http 相关包可以设置代理。
    pdog18
        2
    pdog18  
    OP
       2021-10-16 14:09:11 +08:00
    我不太清楚这个 js 脚本里面的 node fetch API 从哪进行联网的,咋才可以把它走的那条网络让代理“覆盖”到,就像 termial 设置一下 https_proxy 之类的
    makelove
        3
    makelove  
       2021-10-16 15:05:39 +08:00   ❤️ 1
    import HttpsProxyAgent from 'https-proxy-agent'
    const agent = proxy ? new HttpsProxyAgent(proxy) : undefined
    const res = await fetch(url, { agent })
    pdog18
        4
    pdog18  
    OP
       2021-10-16 15:10:38 +08:00
    @makelove 感谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3001 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 14:49 · PVG 22:49 · LAX 07:49 · JFK 10:49
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.