% nvm install v0.6.6
でMac Book Air / Lion にインストールを試みた。
何度もlibv8のインストール(?)のあたりでつまづいた。
いろいろ調べたら、bashじゃないとダメよ的なことだったようで、
bashに切り替えて再度インストールしてみたらあっけなく成功した。
追求する気はないけど、なんで今時実行時のsh環境に左右されるようなインストール環境になっちゃってるんだろう?
しばらくほったらかしになってましたが、気付けば新しくHatena BlogがBetaスタートしたんですね。どなたか招待してもらえると嬉しいです。
node.jsをいじりはじめないといけないなと思い、ようやく触り始めた。いろいろフレームワークも作られてるようでとりあえずひとつ、express-on-railwayをいじってみた。フレームワークの上にフレームワークを作ってるみたいだけども。
https://github.com/1602/express-on-railway
をインストールして、
npmで
をインストール。
で、
$ git clone git://github.com/1602/express-on-railway.git $ cd express-on-railway $ npm install
% railway init sample
現時点で僕の環境ではexpress2.2.2がインストールされていたのだけど、express-on-railwayで想定しているexpressのバージョンは1.0.0のようで、2.2.2だとうまく動かなかった。server.jsのapp.configureのあたりを少し修正するとうごいた。
app.configure(function(){ // app.use(express.staticProvider(__dirname + '/public')); app.use(express.static(__dirname + '/public')); // Providerはいらない app.set('views', __dirname + '/app/views'); app.set('view engine', 'ejs'); // app.use(express.bodyDecoder()); app.use(express.bodyParser()); // DecoderじゃなくてParser // app.use(express.cookieDecoder()); app.use(express.cookieParser()); // DecoderじゃなくてParser // app.use(express.session({store: mongoSessionStore})); app.use(express.session({secret:"sample", store: mongoSessionStore})); // secret属性を追加 app.use(express.methodOverride()); app.use(app.router); });
デフォルトだとconnect-mongodbをドライバとして、mongooseをORマッパ風の便利ライブラリとしてmongoDBを使うみたい。mongoDBは使ったことがないし、モデルの定義はどう書くんだろうと思ったけど、
% railway generate model Comment title:String body:String date:Date
とかすると、db/schema.js に
/** * Comment */ var CommentSchema = new Schema; CommentSchema.add({ body: { type: String }, title: { type: String }, date: { type: Date } }); mongoose.model("Comment", CommentSchema); module.exports["Comment"] = mongoose.model("Comment");
ってのが追記されて、それだけでとりあえずcontroller内のコードとかrailwayコンソール(% railway cでコンソールが使える)でCommentモデルが使えるようになってた。
% railway generate controller sample home post
とすると、/app/controllers/sample_controller.jsができて中身が
action("home", function(){ }); action("post", function(){ });
となっている。
action("home", function(){ Comment.find({}, function(err, comments){ render({comment: new Comment(), title:"test!", comments:comments}); }); }); action("post", function(){ var comment_body = req.body.comment; var comment = new Comment(); comment.body = comment_body; comment.save(function(err){ redirect('/'); }); });
こんな感じでコントローラ内でやりたいことを適当に書く。
exports.routes = function (map) { map.get('/', 'sample#home'); map.post('/post', 'sample#post'); };
とりあえずこんな感じ。
/app/views/sample/home.ejs がすでにできてるので適当に埋める。
<h1><%= title %></h1> <ul> <% comments.forEach(function(comment){ %> <li><%= comment.body %></li> <% }); %> </ul> <% form_for(comment, {action:'/post'}, function(f){ %> <%- f.input('comment') %> <%- f.submit('comment') %> <% }); %>
<%= xxx %>
とかくとhtmlエスケープされて出力
<%- xxx %>
と書くとhtmlエスケープされずに出力されるみたい。
//(1) 'ABCDEF'.charAt(3); // D //(2) ('ABCDEF'.charAt)(3); // D 個人的にはこれもそうなんだってちょっとだけ驚いた。
//(3) var f0 = 'ABCDEF'.charAt; f0(3); // j //(4) var f1 = ('ABCDEF'.charAt); f1(3); // j //(5) (0, 'ABCDEF'.charAt)(3); // j //(6) (x='ABCDEF'.charAt)(3); // j // j なのは charAt実行時のthisがglobalオブジェクトになり、それをtoStringしたものが"[object Object]"になるから。
(2)と(4)はものすごくよく似てるけれど、(2)では()式で関数値が値として評価されてその関数がそのまま関数呼び出しに使われるからthis値がセットされない、と思ったのだけどそうではなく、this値がセットされる関数呼び出しになるんですね。という驚きがあったのでメモ。
San Joseに来てからおよそ二ヶ月。
今日ついに初めての雨が降った。
記念に記録。
でも昼前にすぐに晴れた。