Create proxy server to NPM registry

1.5k Views Asked by At

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

There are 1 best solutions below

9
On BEST ANSWER

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