zoukankan      html  css  js  c++  java
  • npm穿墙

    GWF 很给力,很多东西都能墙掉,但是把 npm 也纳入黑名单,不知道 GWFer 是怎么想的。翻墙翻了好多年了,原理其实也挺简单的,proxy 嘛!

    » 方法一

    A) 国内源,http://cnpmjs.org

    使用方式,你可以在 cmd 中键入 npm install -g cnpm,然后出去吃个饭,如果还没有安装好,那就换个方式:

    npm install -g cnpm --registry=http://r.cnpmjs.org

    registry 参数的作用就是指向需要 download 的仓库。 cnpm 跟国外的 npm 是同步的,只要 npm 有更新,cnpm 就会跟着一起更新。

    当然,你也可以简单点搞:

    npm config set registry="http://r.cnpmjs.org"

    在配置中直接指定源头,下次就没有必要使用 --registry 参数了。配置好了之后,npm 就指向了国内的仓库。

    B)  你也可以直接安装 cnpm,安装好了之后使用 cnpm 来下载文件,其实原理跟上面是一样的,于是你就可以这样了:

    cnpm install -g package_name

    » 方法二

    代理,在配置中设置代理参数:

    # 全局路径,也就是 npm install -g,这里 -g 的意义
    npm config set prefix="c:
    odejs"
    # 一般使用 goagent 翻墙,他的默认端口是 8087 npm config set proxy=http://127.0.0.1:8087
    # 设置 https 的代理 npm config set https_proxy=http://127.0.0.1:8087
    # 这个地方记得设置下,别搞了个代理,结果在国内源下载 npm config set registry=http://registry.npmjs.org

    这样配置好了之后,打开你的 goagent ,记得一定要打开,不然 npm 必然报错。上面写了一堆,其实没必要跟着写这么多,一句话就可以搞定:

    npm config set proxy=http://127.0.0.1:8087

    为啥呢,npm -g 没必要自己去配置, registry 默认就是 http://registry.npmjs.org,不配置 https_proxy,也走的通,所以就只剩下上面这条命令了。

    P.S:有些公司也会提供一个翻墙的主机,可以把上面的代理改成 他 http://IP:Port,也可以翻墙了。翻墙的原理比较简单,就是一个 ”CONNECT“ 请求建立链接,这是一个 node 写的代理,几行代码就搞定了。

    var http = require('http');
    var net = require('net');
    var url = require('url');
    
    // Create an HTTP tunneling proxy
    var proxy = http.createServer(function (req, res) {
      res.writeHead(200, {'Content-Type': 'text/plain'});
      res.end('okay');
    });
    proxy.on('connect', function(req, cltSocket, head) {
      // connect to an origin server
      var srvUrl = url.parse('http://' + req.url);
      var srvSocket = net.connect(srvUrl.port, srvUrl.hostname, function() {
        cltSocket.write('HTTP/1.1 200 Connection Established
    ' +
                        'Proxy-agent: Node-Proxy
    ' +
                        '
    ');
        srvSocket.write(head);
        srvSocket.pipe(cltSocket);
        cltSocket.pipe(srvSocket);
      });
    });
    
    // now that proxy is running
    proxy.listen(1337, '127.0.0.1', function() {
    
      // make a request to a tunneling proxy
      var options = {
        port: 1337,
        hostname: '127.0.0.1',
        method: 'CONNECT',
        path: 'www.google.com:80'
      };
    
      var req = http.request(options);
      req.end();
    
      req.on('connect', function(res, socket, head) {
        console.log('got connected!');
    
        // make a request over an HTTP tunnel
        socket.write('GET / HTTP/1.1
    ' +
                     'Host: www.google.com:80
    ' +
                     'Connection: close
    ' +
                     '
    ');
        socket.on('data', function(chunk) {
          console.log(chunk.toString());
        });
        socket.on('end', function() {
          proxy.close();
        });
      });
    });
    node proxy

    » 方法三

    直接下载到本地。

    实在是怕麻烦,就直接把文件 download 下来,然后放到 node_module 之中就行了。如果是全局模块,找到全局 node_module 的位置,然后解压放进去就行了。

    本文同步自我的微分享

  • 相关阅读:
    02_java基础学习_基础语法(上)01_day02总结
    EditPlus如何设置保存时不产生.bak备份文件?
    UltraEdit(UE)如何设置去掉.bak备份文件?
    如何在win10上连接苹果无线键盘
    01_java基础学习_Java概述_day01总结
    Python 提取Twitter tweets中的元素(包括text, screen names, hashtags)
    #leetcode#Path Sum II
    怎样实现广度优先遍历(BFS)
    GCD编程-串行队列与并发队列
    在对方电脑建立IPC连接, 利用IPC$入侵 运行木马
  • 原文地址:https://www.cnblogs.com/hustskyking/p/npm-cross-wall.html
Copyright © 2011-2022 走看看