Create proxy server to NPM registry

旧巷老猫 提交于 2019-12-11 15:56:18

问题


I am trying to create my own verion of local-npm

I have this simple http server:

#!/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);

In a local terminal, I run:

npm config set registry "localhost:3441"

and just for kicks I run this too:

npm set registry "localhost:3441"

and to confirm it worked, I do:

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

but then when I run npm install, nothing gets intercepted by the proxy, everything just goes to NPM.

What am I doing wrong?


回答1:


So setting the registry is a bit different to a proxy.

Setting the registry will request a package from the set registry; if it is set to the default, npm registry will receive a request, however if something else is set, that will receive request.

Alternatively, setting the proxy will allow the set registry to be accessed via a certain domain. This is what I have to set at work for npm to work.

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

ref


Setting the registry is pretty much the same.

npm config set registry http://localhost:3441


来源:https://stackoverflow.com/questions/50341587/create-proxy-server-to-npm-registry

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!