npm install level 時にエラー

$npm install -save level
...
...
...

In file included from ../src/batch.cc:5:
../node_modules/nan/nan.h:680:46: error: call to non-static member function without an object argument
    return NanPersistentToLocal(handle)->Get(NanSymbol("callback"))
                                             ^~~~~~~~~~~~~~~~~~~~~
../node_modules/nan/nan.h:159:38: note: expanded from macro 'NanSymbol'
#define NanSymbol(value) v8::String::NewSymbol(value)
                         ~~~~~~~~~~~~^~~~~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.
make: *** [Release/obj.target/leveldown/src/batch.o] Error 1
gyp ERR! build error
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:267:23)
gyp ERR! stack     at ChildProcess.emit (events.js:110:17)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (child_process.js:1059:12)
gyp ERR! System Darwin 14.0.0
gyp ERR! command "node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/koya/dev/nakama-player/node_modules/leveldown
gyp ERR! node -v v0.11.14
gyp ERR! node-gyp -v v1.0.2
gyp ERR! not ok
npm ERR! Darwin 14.0.0
npm ERR! argv "node" "/usr/local/bin/npm" "install" "levelup" "leveldown@0.10"
npm ERR! node v0.11.14
npm ERR! npm  v2.1.14
npm ERR! code ELIFECYCLE

npm ERR! leveldown@0.10.2 install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the leveldown@0.10.2 install script 'node-gyp rebuild'.
npm ERR! This is most likely a problem with the leveldown package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls leveldown
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     /Users/koya/dev/nakama-player/npm-debug.log

どうやらnodeのバージョンが悪いらしい

$node -v
v0.11.14
$nodebrew install stable
$nodebrew list
v0.10.35
v0.11.14

current: v0.10.35
$nodebrew use v0.10.35
$node -v
v0.10.35

改めて

$npm install -save level

これでok

nodejsでhttpsサーバーを立てる


node.js | HTTPSサーバーの作り方 - 生存戦略型プログラミング

を参考にさせていただいたところ、

Error: error:0906406D:PEM routines:PEM_def_callback:problems getting password
    at Object.exports.createCredentials (crypto.js:100:17)
    at Server (tls.js:1127:28)
    at new Server (https.js:35:14)
    at Object.exports.createServer (https.js:54:10)
...
...

のようにエラーが出てしまった。

//:before
var options = {
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.crt'),
};
//:after
var options = {
    key: fs.readFileSync('server.key'),
    cert: fs.readFileSync('server.crt'),
    passphrase: 'passphrase'
};

のように鍵を作成した時に設定した、passphraseを追加したらうまく行った。

nodejs で leveldb を利用する

コンパイルの方法とかいろいろ探してしまったけど、そんなの必要なかった。。。

githubのサンプルコードを動かしてみる。

var levelup = require('levelup')

// 1) Create our database, supply location and options.
//    This will create or open the underlying LevelDB store.
var db = levelup('./mydb')

// 2) put a key & value
db.put('name', 'LevelUP', function (err) {
  if (err) return console.log('Ooops!', err) // some kind of I/O error

  // 3) fetch by key
  db.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err) // likely the key was not found

    // ta da!
    console.log('name=' + value)
  })
})
name=LevelUP

次に永続化されているか確認してみる

var levelup = require('levelup')

var db = levelup('./mydb')

db.get('name', function (err, value) {
    if (err) return console.log('Ooops!', err)

    console.log('name=' + value)
  })
name=LevelUP

すげー。

Berkshelf + Vagrant で Node.js + MongoDB の仮想環境構築

今更ながら、他のBerkshelfの記事がわかりづらかったので書いていこうと思う。

環境

chef-dkのインストール

Chef Development Kit | Chef Downloads | Chef
から環境にあったものをダウンロード&インストール。

できたら確認する。

$ berks -v
3.2.1

$ knife -v
Chef: 12.0.3

$ chef -v
Chef Development Kit Version: 0.3.5

Berkshelfでcookbookを用意する

まず自分用のchefリポジトリを用意します。

$ berks cookbook chef-repo
$ ls
chef-repo

そしたらまずはリポジトリの中に入ってbundle install

$ cd chef-repo
$ bundle install

リポジトリ生成時に生成されたBerksfileを以下のように編集する。

source "https://supermarket.getchef.com"

metadata

cookbook 'nodejs'
cookbook 'mongodb'
cookbook 'npm'

ここで

$ berks install

を実行すると Berksfile に書かれた cookbook を ~/.berkshelf にインストールします。
これらの cookbook を指定した場所に持ってくるには

$ berks vendor ./recipes
$ ls ./recipes
7-zip           ark             chef_handler    mongodb         python          yum
apt             build-essential cookbook        nodejs          windows         yum-epel

を実行します。
berks vendor PATH で指定した PATH に持ってこれます。

Vagrantfileの設定

最後にVagrantfileの設定をしたいと思います。
今回はこんな感じにしました。

# -*- mode: ruby -*-
# vi: set ft=ruby :

# Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
VAGRANTFILE_API_VERSION = '2'

Vagrant.require_version '>= 1.5.0'

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|

  config.vm.hostname = 'cookbook-berkshelf'

  # ubuntu 14.04 LST
  config.vm.box = "ubuntu-server-trusty"
  config.vm.box_url = "https://cloud-images.ubuntu.com/vagrant/trusty/current/trusty-server-cloudimg-amd64-vagrant-disk1.box"

  # DHCP使用のゲストの port 3000とホストの port 3000 をマッピング
  config.vm.network :forwarded_port, guest: 3000, host: 3000
  config.vm.network :private_network, type: 'dhcp'

  # 共有フォルダの設定
  config.vm.synced_folder "./develop", "/vagrant"
  
  # provider 設定
  config.vm.provider :virtualbox do |vb|
    vb.customize ["modifyvm", :id, "--memory", "512"]
  end

  # berkshelfのプラグインを使わない。これ重要
  config.berkshelf.enabled = false

  config.vm.provision :chef_solo do |chef|
    chef.cookbooks_path = "./chef-repo/recipes"
    chef.run_list = [
      "recipe[nodejs]",
      "recipe[mongodb]"
    ]
    #vagrant-omnibusで使うchefの設定。Vagrantfileの最後のendのすぐ上に追記しておく。
    config.omnibus.chef_version = :latest
  end
end

Vagrant の Berkshelt のプラグインはいまいち調子が悪いのでここでは使わないようにします。
編集が終わったら起動しましょう。

$ vagrant up

無事エラーなく起動したら

$ vagrant ssh
vagrant@cookbook-berkshelf:~$
vagrant@cookbook-berkshelf:~$ node -v
v0.10.34
vagrant@cookbook-berkshelf:~$ npm -v
1.4.28
vagrant@cookbook-berkshelf:~$ mongo
MongoDB shell version: 2.4.9
connecting to: tes

以上です〜♪

bootstrapのmodal dialogを動的に変化させる

bootstrapのmodal dialogの具体的な使い方は公式で
http://getbootstrap.com/javascript/#modals

まずは公式のをカスタマイズしたソースを載せておきます。

<script>
$(function(){
  $(function(){
  $('#modal-trigger').hide();
  $('#dummy').on('click', function(){
    $('.modal-dialog').find('.modal-body').html('<h1>Hello world!</h1>');
    $('.modal').modal('show');
  });
});
});
</script>

<button type="button" id="dummy">show</button>

<!-- Button trigger modal -->
<button type="button" id="modal-trigger" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
</button>

<!-- Modal -->
<button id="aaaaa">aaaaa</button>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title" id="myModalLabel">Modal title</h4>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>
解説
$('#myModal').modal('show');

で手動で表示させることができます

<button type="button" id="dummy">show</button>

ダミーを用意しておいて

  $('#dummy').on('click', function(){
    $('.modal-dialog').find('.modal-body').html('<h1>Hello world!</h1>');
    $('.modal').modal('show');
  });

表示させる前に表示させる要素の中身を変更させます。

Android StudioでVolleyを使ってみる

Volley.jarの作成

$cd ~/Development
$git clone https://android.googlesource.com/platform/$frameworks/volley
$cd volley

ターゲットを確認

$android list target
Available Android targets:
----------
id: 1 or "android-19"
     Name: Android 4.4.2
     Type: Platform
     API level: 19
     Revision: 3
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in
 Tag/ABIs : no ABIs.
----------
id: 2 or "android-20"
     Name: Android 4.4W
     Type: Platform
     API level: 20
     Revision: 1
     Skins: HVGA, QVGA, WQVGA400, WQVGA432, WSVGA, WVGA800 (default), WVGA854, WXGA720, WXGA800, WXGA800-7in, AndroidWearRound, AndroidWearSquare, AndroidWearRound, AndroidWearSquare
 Tag/ABIs : android-wear/armeabi-v7a, android-wear/x86

今回は4.4.2のid:1を指定

$android update project -p . -t 1

最後にvolley.jar作成

$ant jar
zsh: permission denied: ant

antが入ってないみたい

$brew update
$brew install ant

改めて

$ant jar
Buildfile: /Users/koya/dev/volley/build.xml
...
...
BUILD SUCCESSFUL
Total time: 3 seconds

これで./bin/volley.jarができた

Android Studioに導入

プロジェクトディレクトリー/app/libs/に先ほど作ったvolley.jarをコピー

ProjectStructureでvolley.jarを読み込ませる