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

2021-12-25 00:47:38 +08:00
 chemzqm

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

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

7476 次点击
所在节点    Node.js
8 条回复
mazai
2021-12-25 01:51:05 +08:00
代理是为了下载东西?
ayase252
2021-12-25 02:12:46 +08:00
有可复现的代码吗?
vance123
2021-12-25 02:58:46 +08:00
我今天也遇到类似的问题,发现是因为 npm 包的版本太老,不支持 node16 ,修改 npm 包版本后就好了
muzuiget
2021-12-25 19:52:58 +08:00
确定 http_proxy 和 https_proxy 都试过?理论上 http 代理只是用 http 协议做握手的。
chemzqm
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
2021-12-28 19:52:24 +08:00
有一定失败的 url 吗?
XTTX
2022-01-09 20:33:52 +08:00
我昨天碰到一个窘境,node-sass 需要 node-gyp ,node-gyp 又有一大堆依赖,同时还不支持 node 15 往上。 最直接的方式就是弄个 docker image, 把代码考进去,每次跑容器就好了
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 不是没有理由的, 越来越多的版本和各式各样的依赖,只要一个不对口,整个项目就跑不起来

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/824338

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX