nodejitsuでflatiron (インストール~デプロイ)

nodejitsu.comから直リンされてるクレームワークflatironをインストール。

$ npm install flatiron -g

で、アプリのスケルトン生成。

$ flatiron create unkoman http
info:    Creating application unkoman
info:    Using http scaffold.
prompt: author:  o_6.
prompt: description:  An http service for testing
prompt: homepage:  http://sixdot.hatenablog.com/
info:    Creating directory config
info:    Creating directory lib
info:    Creating directory test
info:    Writing package.json
info:    Writing file app.js
info:    Writing file config/config.json
info:    Application unkoman is now ready

プロンプトの内容はpackage.jsonに反映される。

flatiron create <app_name> <type>

ちなみにcreateコマンドのtypeには「http」「cli」が用意されていて、デフォルトが「http」。 flatiron/scaffoldsに元ネタが入ってる模様。

そのままjitsu deployしてみる。
package.jsonの足りない属性(nodejitsuに必要な属性)は、ちゃんとプロンプトで聞いてくれる。
サブドメインとnodeのバージョンを聞かれた。

$ cd unkoman
$ jitsu deploy
info:    Welcome to Nodejitsu 僕のかっこいいIDネーム
info:    It worked if it ends with Nodejitsu ok
info:    Executing command deploy
warn:    
warn:    Your package.json file is missing required fields:
warn:    
warn:      subdomain,   engines
warn:    
warn:    Prompting user for required fields.
warn:    Press ^C at any time to quit.
warn:    
prompt: subdomain: (IDネーム.unkoman) unkoman
prompt: engines.node: (0.6.x) 0.8.x
...
prompt: Is this ok?: (yes) y
...
info:    Starting app unkoman
info:    App unkoman is now started
info:    http://unkoman.jit.su on Port 80
info:    Nodejitsu ok

さらに虎の子のドメインをpackage.jsonに追加。

"domains" : [ "unkoman.org" ]

更新!

$ jitsu apps update

結果→ http://unkoman.org/

nodejitsuでMongoDB (設定)

nodejitsuにunkoMongoという名前でMongoDBを生成する。

$ jitsu databases create mongo unkoMongo
info:    Welcome to Nodejitsu 僕のかっこいいIDネーム
info:    It worked if it ends with Nodejitsu ok
info:    Executing command databases create mongo unkoMongo
info:    A new mongo has been created
data:    Database Type: mongo
data:    Database Name: unkoMongo
data:    Connection url: mongodb://nodejitsu:ハッシュ@サブドメイン.mongohq.com:ポート番号/データベースID
info:    Nodejitsu ok

↑のConnection urlを使って接続。

あれ?mongoにもパスが通ってないし。いつからこうなった。

$ brew update
...
$ brew install mongodb
...
$ mongo サブドメイン.mongohq.com:ポート番号/データベースID -u nodejitsu -p ハッシュ
MongoDB shell version: 2.2.0
connecting to: サブドメイン.mongohq.com:ポート番号/データベースID
Welcome to the MongoDB shell.

よくできました! o_6.v

Nodejitsuのデプロイエラー

$ jitsu deploy
...
error:   Error running command deploy
error:   Nodejitsu Error (500): Internal Server Error
error:   There was an error while attempting to deploy your application.
error:   
error:   Target script does not exist: /opt/haibu/carapace-versions/0.6.21/node_modules/haibu-carapace/bin/carapace
error:   Error output from Haibu:
...

nodeをv0.7.8 -> v0.8.7にアップデートしてjitsu再インストールしたら直った!

$ nvm install v0.8.7
...
$ nvm use v0.8.7
Now using node v0.8.7
$ sudo npm install jitsu -g

brunch(backbone.js) + Expressでナントカ

基本MVCはbackbone.jsで。node側はなるべくAPIに徹する構成にしたい。

とりあえず生成してみる。

$ express myapp  // Expressアプリ生成
$ cd myapp
$ npm link express jade  // 依存関係とか
$ cd ../
 
$ brunch new  // brunchアプリ生成

現状こんなディレクトリ構成。

brunch  // クライアントサイドアプリ
 - build
    - web  //*** ここではなく ***//
 - config.yaml
 - index.html
 - src
myapp  // Expressアプリ
 - app.js
 - node_modules
 - package.json
 - public  //*** こっちにビルドしたい ***//
 - routes
 - views

そこでCoffeeScriptのビルドパスをmyapp/publicに変更。

/*-- brunch/config.yaml --*/
buildPath:
  myapp/pubic
...

そしてコンパイル。

$ brunch build

さらにindexファイルを移動して、、、

$ mv brunch/index.html myapp/public/web/index.html

それにあわせてパスを修正

<!-- myapp/public/web/index.html -->
...
<link rel="stylesheet" href="/css/main.css" type="text/css" media="screen">
<script src="/js/app.js"></script>
...

/pubic/webのほうを静的に参照させるため、app.jsのstaticを修正。

/* -- myapp/app.js -- */;
...
//app.use(express.static(__dirname + '/pubic'));
app.use(express.static(__dirname + '/pubic/web'));
...
//app.get('/', routes.index);
app.get('/api', routes.index);
...

起動してみる。

$ node myapp/app.js

http://localhost:3000 -> brunchアプリを参照
http://localhost:3000/api -> Expressアプリを参照

よし。おk。