nodejitsu推出私有npm仓库托管服务

基于如下理由,企业需要私有的npm仓库。

  • 确保npm服务快速、稳定:对于企业来说,上线生产系统的时候,需要花半小时甚至更久等待npm模块依赖安装完毕,是不可接受的。部署镜像后,可以确保高速、稳定的npm服务。
  • 发布私有模块:官方的npm上的模块全部是开源的。一些与企业业务逻辑相关的模块可能不适合开源。这部分私有的模块放在私有NPM仓库中,使用起来各种方便。
  • 控制npm模块质量和安全:npm上的模块质量参差不齐,搭建私有仓库,可以更严格地控制模块的质量和安全,只有经过审核的模块才允许被加入私有仓库。

然而,架设私有npm仓库并不容易,需要耗费大量精力。最近nodejitsu 开始私有npm仓库托管服务,从$100/月起步,按照托管的包数量收费。

使用

使用nodejitsu的服务很简单,注册之后修改npm配置即可:

npm config set registry "http://[your-subdomain].registry.nodejitsu.com"

你可以访问 http://[your-subdomain].registry.nodejitsu.com/manage 设定权限控制。

《nodejitsu推出私有npm仓库托管服务》

注意,私有仓库中没有的公开的模块,会通过代理的方式访问公开仓库,十分智能。

smart-private-npm

如果你打算自行架设npm私有仓库,可以使用nodejitsu开源出来的smart-private-npm,这样的话你只需将私有的模块放在私有仓库中,公开的模块可以通过代理访问公开仓库。

var smartPrivateNpm = require("smart-private-npm"),
    url = require("url");

//
// Configure your private npm. You could load this in from a file
// somewhere.
//
var config = {
  rewrites: require("./config/rewrites"),
  proxy: {
    //
    // Location of the target public npm registry. 
    //
    npm: url.parse("http://user:pass@registry.nodejitsu.com"),
    //
    // Private npm options.
    //
    policy: {
      npm: url.parse("http://user:pass@private.registry.nodejitsu.com"),
      private: {
        //
        // This is the list of 'known private modules'
        // that will always be proxied to the private npm.
        // It is built over time by remembering 'publish' requests.
        //
      },
      blacklist: {
        //
        // This is the list of modules that will ALWAYS be proxies
        // to the private npm, no matter what.
        //
      },
      whitelist: {
        //
        // If enabled: only requests for these modules will be served
        // by the proxy (unless they are 'known private modules').
        //
      },
      //
      // In 'transparent mode' the proxy will always forward to
      // the public registry.
      //
      transparent: false
    }
  },
  //
  // Server options (from 'create-servers')
  //
  http: 80
  https: {
    port: 443,
    root: "/path/to/your/ssl/files",
    key: "your-ssl.key",  // or .pem
    key: "your-ssl.cert", // or .pem
  }
};

smartPrivateNpm.createServer(config, function (err, servers) {
  if (err) {
    console.log("Error starting private npm: %j", servers);
    return process.exit(1);
  }

  console.log("Private npm running on %j servers.", Object.keys(servers));
});

架设好服务后,可以通过如下方式发布私有模块:

npm publish some-private-code --reg http://localhost/

除了nodejitsu之外,架设私有仓库还可以考虑阿里开源的cnpm方案。

撰文 SegmentFault

    原文作者:weakish
    原文地址: https://segmentfault.com/a/1190000000398319
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞