目标
npm.xxx.com 安装和推送nodejs包
npmui.xxx.com 管理已经推送的nodejs包
安装 couchdb
https://launchpad.net/~couchdb/+archive/ubuntu/stable
里面有 14.10 14.04 和 12.04的 PPA地址 安装不在这里讨论
安装 couchdb 1.6.1
1.初始化
访问“http://localhost:5984/_utils/”
新建名字为 registry 库
异步 同步官方nodejs模块 (可以不做)
npm curl -X POST http://127.0.0.1:5984/_replicate -d '{"source":"http://registry.npmjs.org/registry/", "target":"registry", "continuous":true, "create_target":true}' -H "Content-Type: application/json"
2.修改配置
/usr/local/etc/couchdb/local.ini[couch_httpd_auth] public_fields = appdotnet, avatar, avatarMedium, avatarLarge, date, email, fields, freenode, fullname, github, homepage, name, roles, twitter, type, _id, _rev users_db_public = true [httpd] bind_address = 0.0.0.0 secure_rewrites = false [couchdb] delayed_commits = false [admins]admin = password
3.上传npm程序
git clone git://github.com/npm/npm-registry-couchapp cd npm-registry-couchapp sudo npm install -g couchapp npm install npm start --npm-registry-couchapp:couch=http://admin:password@localhost:5984/registry npm run load --npm-registry-couchapp:couch=http://admin:password@localhost:5984/registry npm run copy --npm-registry-couchapp:couch=http://admin:password@localhost:5984/registry couchapp push www/app.js http://admin:password@localhost:5984/registry
这边推上去的两个程序访问地址:
http://localhost:5984/_utils/registry/_design/app/_rewrite
http://localhost:5984/_utils/registry/_design/ui/_rewrite
然后只需将registry指向couchdb即可操作私有npm库:
npm --registry http://localhost:5984/registry/_design/app/_rewrite login npm --registry http://localhost:5984/registry/_design/app/_rewrite publish my-repo
私有npm到这里就建好了,链接好长!
于是在/usr/local/etc/couchdb/default.ini文件中添加:
[vhosts] 127.0.0.1:5984=/registry/_design/app/_rewrite127.0.0.2:5984=/registry/_design/ui/_rewrite
这回我们可以用http://127.0.0.1:5984来代替那串长长的链接了
接着就是将私有npm库没有的模块(返回404)通过npmjs.org找,于是写nginx配置如下:
server { listen 80; server_name npm.XXX.com; location / { proxy_set_header Host 127.0.0.1:5984; proxy_pass http://127.0.0.1:5984; proxy_intercept_errors on; error_page 404 = @fallback; } location @fallback{ internal; proxy_set_header Host registry.npmjs.org; proxy_pass http://registry.npmjs.org; }}server { listen 80; server_name npmui.XXX.com; location / { proxy_set_header Host 127.0.0.2:5984; proxy_pass http://127.0.0.2:5984; }}
写在最后
到这里我们的私有库已经搭建完成了
我们可以在 ~/.npmrc 中添加
registry=http://npm.XXX.com/
就可以使用我们的私有库了
npm login #登录 admin password#进入我们的私有包目录 完善package.json 然后npm publish#删除的话是 npm unpublish ***@0.0.0