展开目录
npm设置仓库地址和代理
npm代理proxyhttp仓库
X
陈尼玛的博客
记录开发生涯的踩坑经历,用时间来验证成长
加载中

以cnpm为例,从指定仓库地址安装包:

npm install -g cnpm --registry=https://registry.npm.taobao.org

代理设置目前 没有 试验成功,直接在npm install后面加 --http-proxy 或者修改 .npmrc <font color=red>均不能</font>让npm从代理访问资源。


今天测试了npm的请求,发现全部都是GET方式,因此可以本地搭建一个registry中转服务器,通过中转服务器连接代理访问目标仓库。

  1. 添加hosts

    ```js

  2. 0.0.1 registry.xx.com

  3. 先安装request

    npm install request
  1. 以下代码保存为svr.js

    const request=require('request')
    const r = request.defaults({'proxy':'http://127.0.0.1:8080'})
    process.on('uncaughtException', e=>{
      console.log(e)
    })
    let npm=(registry, local_port)=>{
    const opt=require('url').parse(registry)
    require('http').createServer((req, res)=>{
     r({
       method: req.method,
       uri: (req.url.indexOf(registry)===0?'':registry)+req.url, // 有时候请求的url会带上前缀
       headers: {
         Host: opt.host,
         Connection: 'close',
       }
     }).pipe(res)
    }).listen(local_port)
    }
    npm('http://registry.xx.com', 80)
  2. 运行svr.js

  3. 安装包时修改registry路径为本地端口即可,此时包是通过代理从 https://registry.npmjs.org/ 安装的

    npm install react --registry=http://registry.xx.com

注意:

  1. request使用代理时补上的http头是小写的host,如果是自己写的代理服务器务必处理这个特殊情况,忽略大小写。

  2. 这个中转脚本只监听了http的协议,https的后面遇到再补充。

  3. 部分电脑上不允许直接监听本地80端口,例如mac上需要sudo输入密码才可以启动svr.js。

  4. 代理服务器需要<font color=red>禁止从本地访问npm仓库</font>,否则这个中转服务器就失去意义了。

相关文档

暂无

随便看看

  1. 定长消息队列读写优化

  2. css3自定义滚动条样式

  3. nginx 子域名对应文件夹

  4. npm远程服务器某些配置不兼容代理的解决办法

  5. SSL certificate problem: self signed certificate in certificate chain

  6. ios13 vpn 能连接但不能传数据问题解决

  7. html5 全屏代码

  8. python 用摄像头拍照并写入文件

  9. nodejs俄罗斯方块

  10. mongodb 批量修改字段语句

  11. putty使用http代理连接服务器

  12. ssl 证书生成方式

  13. 猴子选大王算法问题

  14. ie8上Image.onload不触发问题

  15. 简易版事件封装

  16. 数据库清理优化

  17. youku电脑版跳过广告代码

  18. html表格导出csv文件并下载

畅言模块加载中