为 NPM 注册表创建代理服务器

Create proxy server to NPM registry

我正在尝试创建自己的 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。

我做错了什么?

因此设置注册表与代理有点不同。

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

或者,设置代理将允许通过特定域访问设置的注册表。这是我必须在工作中设置的 npm 才能工作。

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

ref


设置注册表几乎相同。

npm config set registry http://localhost:3441