chemzqm
V2EX  ›  Node.js

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

  •  
  •   chemzqm · Dec 25, 2021 · 9110 views
    This topic created in 1774 days ago, the information mentioned may be changed or developed.

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

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

    8 replies  •  2022-01-09 20:40:21 +08:00
    mazai
    mazai
    Dec 25, 2021
    1
    代理是为了下载东西?
    ayase252
    ayase252
    Dec 25, 2021 via iPhone
    2
    有可复现的代码吗?
    vance123
    vance123
    Dec 25, 2021 via Android
    3
    我今天也遇到类似的问题,发现是因为 npm 包的版本太老,不支持 node16 ,修改 npm 包版本后就好了
    muzuiget
    muzuiget
    Dec 25, 2021
    4
    确定 http_proxy 和 https_proxy 都试过?理论上 http 代理只是用 http 协议做握手的。
    chemzqm
    chemzqm
    OP
    Dec 26, 2021
    5
    跟请求的 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
    zbinlin
    Dec 28, 2021
    6
    有一定失败的 url 吗?
    XTTX
    XTTX
    Jan 9, 2022
    7
    我昨天碰到一个窘境,node-sass 需要 node-gyp ,node-gyp 又有一大堆依赖,同时还不支持 node 15 往上。 最直接的方式就是弄个 docker image, 把代码考进去,每次跑容器就好了
    XTTX
    XTTX
    Jan 9, 2022
    8
    ```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 不是没有理由的, 越来越多的版本和各式各样的依赖,只要一个不对口,整个项目就跑不起来
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   5059 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 37ms · UTC 07:58 · PVG 15:58 · LAX 23:58 · JFK 02:58
    ♥ Do have faith in what you're doing.