社内でTower.jsを使いたい

tower.js - http://towerjs.org/

インストールする。

[rossy@centos6 ~]$ su -
[root@centos6 ~]# npm install -g tower
[root@centos6 ~]# exit

ドキュメントに従ってコマンドを打つ。

[rossy@centos6 ~]$ mkdir napps && cd napps
[rossy@centos6 napps]$ tower new tapp
:
events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: getaddrinfo ENOENT
    at errnoException (dns.js:31:11)
    at Object.onanswer [as oncomplete] (dns.js:123:16)

何かエラーが出た。"Error: getaddrinfo ENOENT"で検索すると、とりあえずのパッチで対応できるっぽいので試してみる。

[rossy@centos6 napps]$ su -
[root@centos6 ~]# npm install -g tunnel
[root@centos6 ~]# wget https://gist.github.com/raw/2914780/b55dfd2c57cbfec4f30ffeb5394d974371507ad7/superagent-proxy-patch.diff
[root@centos6 ~]# cd /usr/local/lib/node_modules/npm/
[root@centos6 npm]# patch node_modules/tower/node_modules/superagent/lib/node/index.js ~/superagent-proxy-patch.diff 
[root@centos6 npm] exit

失敗したときのディレクトリを削除してリトライ。

[rossy@centos6 napps]$ export http_proxy=http://proxyHost:proxyPort/
[rossy@centos6 napps]$ rm -rf tapp
[rossy@centos6 napps]$ tower new tapp
:
Error downloading https://raw.github.com/logicalparadox/chai/master/chai.js
:
Error downloading https://raw.github.com/viatropos/tower/master/dist/tower.js
:

エラーが2つ出たけどとりあえず最後までいったっぽい。リンク先が404だったのでとりあえず見なかったことに。

[rossy@centos6 napps]$ cd tapp
[rossy@centos6 tapp]$ npm install
:
npm http GET https://github.com/indexzero/read/tarball/refactor-optional-streams
npm ERR! fetch failed https://github.com/indexzero/read/tarball/refactor-optional-streams
npm http 200 http://registry.npmjs.org/timespan/-/timespan-2.2.0.tgz
npm ERR! Error: SSL Error: Hostname/IP doesn't match certificate's altnames
npm ERR!     at ClientRequest.<anonymous> (/usr/local/lib/node_modules/npm/node_modules/request/main.js:440:26)
n
:

今度は別のエラーが・・・。これはnpm config set strict-ssl falseすればいいらしいのでコマンド叩いてリトライ。

[rossy@centos6 tapp]$ npm config set strict-ssl false
[rossy@centos6 tapp]$ npm install
:
> design.io@0.3.1 install /home/rossy/napps/tapp/node_modules/design.io
> gem install rb-fsevent rb-inotify --no-ri --no-rdoc

sh: gem: コマンドが見つかりません

gemがない・・・というかruby入れてない・・・

[rossy@centos6 tapp]$ su -
[root@centos6 ~]# yum install ruby
[root@centos6 ~]# yum install ruby-devel
[root@centos6 ~]# cd /usr/local/src
[root@centos6 src]# wget http://rubyforge.org/frs/download.php/76073/rubygems-1.8.24.tgz
[root@centos6 src]# tar xvfz rubygems-1.8.24.tgz 
[root@centos6 src]# cd rubygems-1.8.24
[root@centos6 rubygems-1.8.24]# ruby setup.rb 
[root@centos6 rubygems-1.8.24]# exit
[rossy@centos6 tapp]$ echo gem: --user-install > ~/.gemrc

ruby-devel入れておかないとこんなエラーが出て困る。

ERROR:  Error installing rb-inotify:
	ERROR: Failed to build gem native extension.

        /usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /usr/lib/ruby/ruby.h

リトライ。

[rossy@centos6 tapp]$ npm install

無事終了

先人達に感謝。
参考:
tower.js を試用していきなりつまずきました、問題解決のヒントをいただけないでしょうか
Issue #2719: npm fails behind a proxy since node 0.8.4 · isaacs/npm
gem installでsudoしたくないでござる絶対にsudoしたくないでござる

mocha + jscoverageを動かす

jscoverage - https://github.com/visionmedia/node-jscoverage

インストール

上記ページに下の方にnpmのは動かないって書いてあった。

Warning: the jscoverage npm module published by someone else does not work, clone this repo.

なのでgithubから落としてインストール。

[rossy@centos6 napp]$ su -
[root@centos6 ~]# cd /usr/local/src/
[root@centos6 src]# wget https://github.com/visionmedia/node-jscoverage/zipball/master -O node-jscoverage.zip
[root@centos6 src]# unzip node-jscoverage.zip
[root@centos6 src]# cd visionmedia-node-jscoverage-36b3ba5/
[root@centos6 visionmedia-node-jscoverage-36b3ba5]# ./configure
[root@centos6 visionmedia-node-jscoverage-36b3ba5]# make
[root@centos6 visionmedia-node-jscoverage-36b3ba5]# make install
[root@centos6 visionmedia-node-jscoverage-36b3ba5]# exit

準備

足し算のモジュールを用意して確認する。

[rossy@centos6 napp]$ tree
.
|-- lib
|   `-- sample.js
`-- test
    `-- sampleTest.js

2 directories, 2 files

sample.js

exports.plus = function(x, y) {
    return x + y;
};

sampleTest.js

var assert = require("assert")
var module = require('../lib/sample');
describe('sample', function(){
  describe('plus', function(){
    it('should return 3', function() {
      assert.equal(3, module.plus(2, 1));
    })
  })
})

動作確認

jscoverageしてカバレッジ用のコードをlib-covに出力する。

[rossy@centos6 napp]$ jscoverage lib lib-cov
[rossy@centos6 napp]$ tree
.
|-- lib
|   `-- sample.js
|-- lib-cov
|   `-- sample.js
`-- test
    `-- sampleTest.js

3 directories, 3 files

カバレッジ用にrequireの部分を書き換える。
sampleTest.js

var assert = require("assert")
var module = process.env.TEST_COV
   ? require('../lib-cov/sample')
   : require('../lib/sample');
describe('sample', function(){
  describe('plus', function(){
    it('should return 3', function() {
      assert.equal(3, module.plus(2, 1));
    })
  })
})

実行する。

[rossy@centos6 napp]$ TEST_COV=1 mocha --reporter html-cov > coverage.html 

開く。
f:id:sakshr:20120821192020p:plain

mochaのサンプルを動かす

Mocha - http://visionmedia.github.com/mocha/

インストール

[rossy@centos6 ~]$ su -
[root@centos6 ~]# npm install -g mocha
[root@centos6 ~]# exit

動作確認

[rossy@centos6 ~]$ mkdir napp
[rossy@centos6 napp]$ mkdir test
[rossy@centos6 napp]$ vi test/test.js

test/test.js

var assert = require("assert")
describe('Array', function(){
  describe('#indexOf()', function(){
    it('should return -1 when the value is not present', function(){
      assert.equal(-1, [1,2,3].indexOf(5));
      assert.equal(-1, [1,2,3].indexOf(0));
    })
  })
})

テストを実行する。個人的にはreporter specの出力が好み。

[rossy@centos6 napp]$ mocha

  ․

  ✔ 1 test complete (2ms)

[rossy@centos6 napp]$ mocha --reporter spec


  Array
    #indexOf()
      ✓ should return -1 when the value is not present 


  ✔ 1 test complete (2ms)

centos 6 で node.js を動作させる

CentOS 6 は Minimal Desktop でインストールした。

準備

とりあえずwgetとyumのproxy設定。

suしておく

[rossy@centos6 ~]$ su -

wget

wgetrcのproxyのコメントアウトを外して修正する。

[root@centos6 ~]# vi /etc/wgetrc
:
https_proxy = http://proxyHost:proxyPort/
http_proxy = http://proxyHost:proxyPort/
ftp_proxy = http://proxyHost:proxyPort/
:

yum

yum.confの最後に1行追加

[root@centos6 ~]# vi /etc/yum.conf
:
proxy=http://proxyHost:proxyPort/

gccとg++

Minimal Desktopでインストールするとgccとg++が入ってないので入れておく。

[root@centos6 ~]# yum install gcc-c++

node.jsのインストール

ダウンロード

[root@centos6 ~]# cd /usr/local/src
[root@centos6 src]# wget http://nodejs.org/dist/v0.8.7/node-v0.8.7.tar.gz

インストール

[root@centos6 src]# tar xvfz node-v0.8.7.tar.gz
[root@centos6 src]# cd node-v0.8.7
[root@centos6 node-v0.8.7]# ./configure
[root@centos6 node-v0.8.7]# make
[root@centos6 node-v0.8.7]# make install

インストール後の設定

[root@centos6 ~]# npm set proxy http://proxyHost:proxyPort/
[root@centos6 ~]# npm config set registry http://registry.npmjs.org/

npm config set registry http://registry.npmjs.org/

インストール作業が終わったら抜けておく。

[root@centos6 node-v0.8.7]# exit

動作確認

[rossy@centos6 ~]$ mkdir nodeApps
[rossy@centos6 ~]$ cd nodeApps/
[rossy@centos6 nodeApps]$ vi example.js

example.js(サンプルのコピペ)

var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

実行する。

[rossy@centos6 nodeApps]$ node example.js 
Server running at http://127.0.0.1:1337/