创建代理服务器到 NPM 注册表 [英] Create proxy server to NPM registry
问题描述
我正在尝试创建自己的 local-npm 版本
I am trying to create my own verion of local-npm
我有这个简单的 http 服务器:
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"
我也跑这个:
npm set registry "localhost:3441"
为了确认它有效,我这样做:
and to confirm it worked, I do:
$(npm get registry) => "localhost:3441"
但是当我运行 npm install
时,代理没有拦截任何东西,一切都转到 NPM.
but then when I run npm install
, nothing gets intercepted by the proxy, everything just goes to NPM.
我做错了什么?
推荐答案
所以设置注册表与代理有点不同.
So setting the registry is a bit different to a proxy.
设置注册表会从设置的注册表中请求一个包;如果设置为默认值,npm registry 将收到一个请求,但是如果设置了其他内容,它将收到请求.
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.
或者,设置代理将允许通过某个域访问设置的注册表.这是我必须在工作中设置的 npm 才能工作.
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
设置注册表几乎相同.
Setting the registry is pretty much the same.
npm config set registry http://localhost:3441
这篇关于创建代理服务器到 NPM 注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!