0

我正在尝试创建自己的 local-npm 版本

我有这个简单的 http 服务器:

#!/usr/bin/env node
'use strict';

import http = require('http');

const s = http.createServer(function (clientRequest, clientResponse) {

  if (clientRequest.url === 'x') {
    clientResponse.write('retrieve the tarball from local fs');
    clientResponse.end();
    return;
  }

  const proxy = http.request({
      hostname: 'https://registry.npmjs.org',
      port: 80,
      path: clientRequest.url,
      method: clientRequest.method
    },
    function (res) {
      res.pipe(clientResponse);
    });

  clientRequest.pipe(proxy);

});

s.listen(3441);

在本地终端中,我运行:

npm config set registry "localhost:3441"

只是为了踢我也运行这个:

npm set registry "localhost:3441"

并确认它有效,我这样做:

$(npm get registry) => "localhost:3441"

但是当我运行时npm install,代理不会拦截任何内容,一切都只是转到 NPM。

我究竟做错了什么?

4

1 回答 1

1

所以设置注册表与代理有点不同。

设置注册表会从设置的注册表中请求一个包;如果设置为默认值,npm 注册表将接收请求,但是如果设置了其他内容,则将接收请求。

或者,设置代理将允许通过某个域访问设置的注册表。这是我必须在工作中设置的内容才能使 npm 正常工作。

npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080

参考


设置注册表几乎相同。

npm config set registry http://localhost:3441
于 2018-05-15T02:52:32.170 回答