Herokuでこんにちは世界3回目

HerokuでExpressを使ったHello, Worldアプリのレスポンスが悪かった(初回の表示に妙に時間がかかる)ので、Expressを使わないで作ってみたらどうなんだろうと思ってまた試してみたのでした。
でも体感速度しか測ってないから余り意味無し。
環境:MacOS 10.7.4

スタックを作る

$ heroku apps:create tet

cedarスタックがデフォルトなので作るのもラクチンになりました。
testと入力するはずがtetになってしまったという……

Gitリポジトリを作る

$ git init tet

Gitリポジトリをローカルに作成してあとはいろいろファイルを追加します。

app.js
var http = require('http'),
    server = http.createServer();

server.on('request', function (req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/html; charset=utf-8'
  });
  res.end([
    '<!DOCTYPE html>',
    '<meta charset="utf-8">',
    '<title>tet</title>',
    '<style>',
    '  p {text-align: center;}',
    '</style>',
    '<p>インターネット</p>'
  ].join('\n'));
});
server.listen(process.env.PORT || 3000);
package.json
{
  "name": "tet",
  "version": "0.0.1",
  "dependencies": {
  },
  "engines": {
    "node": "0.8.7",
    "npm": "1.1.49"
  }
}
.gitignore
node_modules/
Procfile
web: node app.js

Herokuにリポジトリをプッシュ

$ git remote add heroku git@heroku.com:tet.git
$ git add .
$ git commit -m "initial commit"
$ git push heroku master

体感速度

初回表示?が圧倒的にこちらの方がExpressで作った方よりも速い。
まあ当たり前なんだろうけど…… それでもExpressの方が妙に遅いという感じ。
フレームワークだからいろいろやってるにせよ、それでも遅いなあと思った。
……もしかしてNODE_ENV=productionにしてないからかなー。


Getting Started on Heroku with Node.js | Heroku Dev Centerによると

$ heroku config:add NODE_ENV=production

で良いみたい。
Procfileに

web: NODE_ENV=production node app.js

って書くんじゃだめなのかな、どうなのかな。
でもまあ、あまり良い書き方ではないか。