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

Node 16 无法给 https 请求使用 http 代理了

  •  
  •   chemzqm · 2021-12-25 00:47:38 +08:00 · 7437 次点击
    这是一个创建于 824 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Node 14 是可以的,不清楚有没有人可以搞定,可以付费

    https://github.com/neoclide/coc.nvim/issues/3521

    8 条回复    2022-01-09 20:40:21 +08:00
    mazai
        1
    mazai  
       2021-12-25 01:51:05 +08:00
    代理是为了下载东西?
    ayase252
        2
    ayase252  
       2021-12-25 02:12:46 +08:00 via iPhone
    有可复现的代码吗?
    vance123
        3
    vance123  
       2021-12-25 02:58:46 +08:00 via Android
    我今天也遇到类似的问题,发现是因为 npm 包的版本太老,不支持 node16 ,修改 npm 包版本后就好了
    muzuiget
        4
    muzuiget  
       2021-12-25 19:52:58 +08:00
    确定 http_proxy 和 https_proxy 都试过?理论上 http 代理只是用 http 协议做握手的。
    chemzqm
        5
    chemzqm  
    OP
       2021-12-26 19:38:26 +08:00
    跟请求的 url 也有关系,有些就不会报错,有些偶尔能成功,不过我发现请求其实成功了:
    ```js
    let url = require('url')
    let https = require('https')
    let HttpsProxyAgent = require('https-proxy-agent')

    // HTTP/HTTPS proxy to connect to
    let proxy = 'http://127.0.0.1:7070'
    console.log('using proxy server %j', proxy)

    // HTTPS endpoint for the proxy to connect to
    let endpoint = 'https://registry.npmjs.org/coc-omni'
    console.log('attempting to GET %j', endpoint)
    let options = url.parse(endpoint)

    // create an instance of the `HttpsProxyAgent` class with the proxy server information
    let agent = new HttpsProxyAgent(proxy)
    options.agent = agent

    https.get(options, function (res) {
    console.log('"response" event!', res.headers)
    res.pipe(process.stdout)
    })

    ```
    zbinlin
        6
    zbinlin  
       2021-12-28 19:52:24 +08:00
    有一定失败的 url 吗?
    XTTX
        7
    XTTX  
       2022-01-09 20:33:52 +08:00
    我昨天碰到一个窘境,node-sass 需要 node-gyp ,node-gyp 又有一大堆依赖,同时还不支持 node 15 往上。 最直接的方式就是弄个 docker image, 把代码考进去,每次跑容器就好了
    XTTX
        8
    XTTX  
       2022-01-09 20:40:21 +08:00
    ```dockerfile
    FROM node:14-alpine as base
    ENV HOME=/home/node
    RUN apk add --no-cache python3 make g++ && \
    yarn global add node-gyp@${VERSION} && \
    yarn cache clean && \
    node-gyp help && \
    mkdir $HOME/.cache && \
    chown -R node:node $HOME
    USER node
    VOLUME $HOME/.cache
    WORKDIR $HOME
    CMD ["sh"]

    FROM base
    WORKDIR /frontend
    COPY package.json package.json
    RUN yarn install
    COPY . .
    CMD ["npm", "start"]
    ```

    现在流行 ship with environment 不是没有理由的, 越来越多的版本和各式各样的依赖,只要一个不对口,整个项目就跑不起来
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3255 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 14:16 · PVG 22:16 · LAX 07:16 · JFK 10:16
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.