需求:由于内部公共模块的需求,所以决定搭建一个局域网的内部私有npm仓库,用于企业内部共享模块,快速安装模块等。
npm仓库:npm仓库的设计基于CouchDB实现(CouchDB是一个NoSQL数据库,基于文档设计)。相对于命令喊执行的npm命令,npm仓库是存放模块的服务器。

环境:

  • centos7
  • nodejs v6.10.0
  • npm 3.10.10

安装Erlang

Erlang 是一种编程语言,CouchDB 数据库是由Erlang写成。

源码安装Erlang

$ wget http://www.erlang.org/download/otp_src_20.1.tar.gz  # 这里自行去官网获取准确地址
$ tar -zxvf otp_src_20.1.tar.gz # 解压 
$ cd otp_src_20.1  
$ ./configure  # 环境检测,这里可能需要相关的编译环境,执行安装环境!!
$ make & sudo make install 

再次键入下面的命令,检查是否安装成功

#  erl 
Erlang/OTP 20 [erts-9.1] [source] [64-bit] [smp:8:2] [ds:8:2:10] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V9.1  (abort with ^G)
1> 

安装 CouchDB

在有Erlang环境的情下,CouchDB才能被安装
注意:源码安装总是报:jsapi.h: No such file or directory 错误,所以选择二进制安装

大致步骤如下:

# vim /etc/yum.repos.d/bintray-apache-couchdb-rpm.repo

写入一下内容

[bintray--apache-couchdb-rpm]
name=bintray--apache-couchdb-rpm
baseurl=http://apache.bintray.com/couchdb-rpm/el7/$basearch/
gpgcheck=0
repo_gpgcheck=0
enabled=1

安装:

 yum -y install epel-release && yum install couchdb

详情看官网:http://docs.couchdb.org/en/2.1.1/install/unix.html#installation-using-the-apache-couchdb-convenience-binary-packages (地址将来有可能改变)

启动 CouchDB

# systemctl start  couchdb.service
# curl -i http://127.0.0.1:5984/ #查看服务是否启动正确
显示如下类似信息表示成功启动
HTTP/1.1 200 OK
X-CouchDB-Body-Time: 0
X-Couch-Request-ID: 184b524efc
Server: CouchDB/2.1.1 (Erlang OTP/18)
Date: Thu, 09 Nov 2017 02:47:34 GMT
Content-Type: application/json
Content-Length: 116
Cache-Control: must-revalidate

{"couchdb":"Welcome","version":"2.1.1","features":["scheduler"],"vendor":{"name":"The Apache Software Foundation"}}

搭建npm仓库

之前的工作就绪之后,我们就可以搭建npm仓库了,这一步需要CouchDB一直启动作为服务,搭建npm仓库主要包含如下5步
  1. 创建npm数据库,首先,我们需要调用CouchDB的接口为创库创建一个数据库,之后所有的模块包文件将作为附件博阿春在这个数据库中。
# curl -X PUT http://127.0.0.1:5984/registry 
 {"ok":true} 
  1. 获取npm仓库源码
# git clone git@github.com:npm/npm-registry-couchapp.git
# cd npm-registry-couchapp
  1. 获取安装工具
# npm install couchapp -g 
# npm install couchapp 
# npm install semver 
  1. 转载npm仓库代码到CouchDB中
注意:

push registry/app.js 时需要加 DEPLOY_VERSION='git describe --tags',

否则会报错:
/home/npm-registry-couchapp/registry/app.js:15
throw new Error('Must set DEPLOY_VERSION env to git describe output')
^
Error: Must set DEPLOY_VERSION env to git describe output
详情:https://github.com/npm/npm-registry-couchapp/issues/263

[root@hh npm-registry-couchapp]# DEPLOY_VERSION='git describe --tags'  couchapp push registry/app.js http://127.0.0.1:5984/registry
Preparing.
Serializing.
PUT http://127.0.0.1:5984/registry/_design/scratch
Finished push. 1-222fc0325025e06944e67c5b8ae00ed1
[root@hh npm-registry-couchapp]#  couchapp push www/app.js http://127.0.0.1:5984/registry 
Preparing.
Serializing.
PUT http://127.0.0.1:5984/registry/_design/ui
Finished push. 2-a0f4a04e9d7835f472a456d315a72901

上述步骤分别将registry和www下的代码放进 CouchDB 的registry库中。

一个本地的npm仓库就此搭建完成了。

访问 http://127.0.0.1:5984/registry/_design/ui/_rewrite 可以看到npm仓库的 web ui 界面
访问 http://127.0.0.1:5984/registry/_design/scratch/_rewrite 则对应的是 JSON API 服务

CouchDB 配置

  • 默认安装 CouchDB 后,将会监听127.0.0.1,这会导致只有当前机器可以访问 CouchDB 服务,改为 0.0.0.0 则可以被外部机器访问到。
  • 访问 http://127.0.0.1:5984/registry/_design/scratch/_rewrite 可能得到 {"error":"insecure_rewrite_rule","reason":"too many ../.. segments"} 这样的错误,修改 CouchDB配置中的 secure_rewrites 为 false 可以解决改问题。

查看配置文件路径(仅限yum安装的软件)

# rpm -qc couchdb # 
/etc/logrotate.d/couchdb
/opt/couchdb/etc/local.ini

/opt/couchdb/etc 还有其他文件 如:

drwxr-xr-x 2 couchdb couchdb  4096 Nov  8 05:21 default.d  # 默认配置文件
-rwxr-xr-x 1 couchdb couchdb 21571 Nov  7 17:57 default.ini
drwxr-xr-x 2 couchdb couchdb  4096 Nov  8 05:21 local.d
-rwxr-xr-x 1 couchdb couchdb  4704 Nov  9 01:56 local.ini
-rwxr-xr-x 1 couchdb couchdb  1793 Nov  7 17:57 vm.args

修改配置文件

# vim /opt/couchdb/etc/local.ini  # local.ini 的配置将覆盖 default.ini 中的同名配置

修改配置为:(分号表示注释)

[httpd]
bind_address = 0.0.0.0  ; 可以被外部访问
secure_rewrites = false  ; 可以解决上面提到的insecure_rewrite_rule错误  
# systemctl restart  couchdb.service # 重启 couchdb 数据库生效配置 

访问数据库 web 界面
http://localhost:5984/_utils/index.html